package jp.co.sega.sonic_jump.monthly.sum;

import android.os.Environment;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.content.res.AssetManager;
import java.nio.file.StandardOpenOption;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardCopyOption;
import android.content.res.AssetFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.SurfaceHolder;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import jp.co.sega.sonic_jump.monthly.sum.PlatformStandard.Paying;
import jp.co.sega.sonic_jump.monthly.sum.PlatformStandard.Standard;
import jp.co.sega.sonic_jump.monthly.sum.R;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.app.Activity;
import java.net.ServerSocket;
import java.io.IOException;
import java.net.Socket;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import java.util.ArrayList;
import android.graphics.Point;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.net.InetAddress;
import java.util.List;
import java.util.Enumeration;
import java.net.NetworkInterface;
import java.net.SocketException;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.*;
import java.io.*;
import java.nio.ByteBuffer;
import android.os.Handler;
import android.os.Looper;
import android.webkit.WebView;
import android.webkit.WebSettings;
import java.util.Locale;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.DisplayMetrics;
import android.webkit.WebViewClient;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.MediaStore;
import java.net.URL;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient.FileChooserParams;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import java.net.URLEncoder;
import java.net.URLDecoder;
import android.view.Choreographer;
import android.content.pm.ActivityInfo;

public class Game_Draw extends Device implements Runnable {
    public CRTView crtView;
    public static boolean shadowstory = false;
    public int browsers = 0;
    public Editor editor;
    public static boolean rotationland;
    public static boolean PauseAdjust;
    public static boolean alt;
    public static boolean BigFont = true;
    public static boolean RegularFont = true;
    public static boolean SmallFont = false;
    public static boolean FontAdjust = true;
    public static boolean FontRightAdjust = false;
    public static boolean helpAdjust = false;
    public static boolean tutAdjust = false;
    public static String CurrentL;
    public DeviceImage[] BlockPicsBackup = new DeviceImage[21];
    public DeviceImage[] EdgePicsBackup  = new DeviceImage[21];
    public DeviceImage[] ExtrasPicBackup = new DeviceImage[21];
    public DeviceImage[] StripPicsBackup = new DeviceImage[21];
    public DeviceImage[] TilesPicsBackup = new DeviceImage[21];
    public DeviceImage[] BlockPics2;
    public DeviceImage[] EdgePics2;
    public DeviceImage[] ExtrasPic2;
    public DeviceImage[] StripPics2;
    public DeviceImage[] TilesPics2;
    public String[] stagename = new String[21];
    public int[] actNumber = new int[21];
    private static final String CHANNEL_ID = "download_channel";
    public static boolean notification = false;
    private String[] rooms = new String[21];
    public int[] life = new int[21];
    public boolean crt;
    public int crt2 = 0;
    public FrameLayout frameLayout;
    double delta = 0;
    int multiplayerh = -1;
    private long lastnanoTime = System.nanoTime();
    public String eventname;
    public boolean customtouchpad = false;
    public boolean newTouch = false;
    public static String eventtextures = "";
    public static boolean event = false;
    public int eventlevel;
    public int eventlevel2;
    public int eventrings = 0;
    public int eventbluehoops = 0;
    public static int bluehoops = 0;
    public int eventbluehoops2 = 0;
    public int eventrings2 = 0;
    public String eventcharacter;
    public String character = "sonic";
    public static String[] currentmusic = new String[21];
    public String eventunb = "";
    public String eventday;
    public boolean aIntro = false;
    public String Event = "https://raw.githubusercontent.com/gdgohan/SonicJumpMapEditor/main/";
    public String OnlineLink = "https://github.com/GdGohan/SonicJumpMapEditor/labels/Custom%20Map";
    public InputStream OnlineIn = null;
    public static boolean Online = false;
    public static boolean JumpPro = false;
    public static boolean Fever = false;
    public boolean DemoFurikan = false;
    public boolean soundtrack = false;
    public boolean sonic = true;
    public int sp = 0;
    public MediaPlayer mediaPlayer = new MediaPlayer();
    //public static int FPSV = 16;
    public static int speedtime = 0;
    public static int speedtime2 = 0;
    public static boolean browser = false;
    public volatile static int tmpSp = 0;
    public boolean PauseGameTouch = false;
    public boolean tailsfly = false;
    public boolean tails = false;
    public boolean shadow = false;
    public boolean shadowt = false;
    private static final int BOSSMODE_DIE = 2;
    private static final int BOSSMODE_ENTER = 0;
    private static final int BOSSMODE_GO = 1;
    private static final int BOSSMODE_NONE = -1;
    private static final int BOSSMODE_SCARPER = 3;
    static final int BRAND_ROTATION = 29;
    private static final int COLL_NME_BOT = -15;
    private static final int COLL_NME_LEFT = -8;
    private static final int COLL_NME_RIGHT = 8;
    private static final int COLL_NME_TOP = -15;
    private static final int COLL_PLAT_LEFT = -6;
    private static final int COLL_PLAT_RIGHT = 30;
    private static final int COLL_last = 0;
    static final int CONFIRMMAIN = 26;
    static final int CUTSCENE = 22;
    static final int CUTSCENEEXIT = 23;
    public static final int Color_Red = 16711680;
    public static final int Color_White = 16777215;
    static final int DELRECORDS = 21;
    static final int DEMO_END = 32;
    static final int DEMO_START = 31;
    private static final int ENEMY_ANTENNA = 14;
    private static final int ENEMY_BLUE_BONUS = 6;
    private static final int ENEMY_BOSS = 20;
    private static final int ENEMY_CHECKPOINT = 8;
    private static final int ENEMY_DEBRIS_RING = 18;
    private static final int ENEMY_DEBRIS_SHARD = 22;
    private static final int ENEMY_EXPLOSION = 19;
    private static final int ENEMY_FLYING_CREATURE = 7;
    private static final int ENEMY_GREEN_BONUS = 5;
    private static final int ENEMY_NONE = -1;
    private static final int ENEMY_PLATFORM = 0;
    private static final int ENEMY_PLATFORM_CRUMBLY = 1;
    private static final int ENEMY_PLATFORM_CRUMBLY_SLIDING = 13;
    private static final int ENEMY_PLATFORM_CRUMBLY_SLIDING_WAITING = 17;
    private static final int ENEMY_PLATFORM_CRUMBLY_WAITING = 16;
    private static final int ENEMY_PLATFORM_SLIDING = 9;
    private static final int ENEMY_PLATFORM_SPIKEY = 2;
    private static final int ENEMY_PLATFORM_SPIKEY_SLIDING = 12;
    private static final int ENEMY_PLATFORM_SPRING = 4;
    private static final int ENEMY_PLATFORM_SPRING_SLIDING = 11;
    private static final int ENEMY_RING = 3;
    private static final int ENEMY_SMOKE = 21;
    private static final int ENEMY_TRACTOR = 10;
    private static final int ENEMY_last = 9999;
    static final int EXITCONFIRM = 8;
    static final int EXITSPLASH = -2;
    static final int FIN = 20;
    static final int GAME = 1;
    private static final int GFX_ALTIMETER_POINTER = 36;
    private static final int GFX_ANTENNA_BASE = 42;
    private static final int GFX_ANTENNA_OFF = 40;
    private static final int GFX_ANTENNA_ON = 41;
    private static final int GFX_BALL = 119;
    private static final int GFX_BALL2 = 121;
    private static final int GFX_BALL3 = 123;
    private static final int GFX_BALL4 = 125;
    private static final int GFX_BALL5 = 126;
    private static final int GFX_BALL6 = 127;
    private static final int GFX_BLUE_BONUS_BACK = 12;
    private static final int GFX_BLUE_BONUS_GO_A = 56;
    private static final int GFX_BOSS_1 = 76;
    private static final int GFX_BOSS_2 = 77;
    private static final int GFX_BOSS_3 = 78;
    private static final int GFX_BOSS_4 = 79;
    private static final int GFX_BOSS_5 = 80;
    private static final int GFX_BOSS_6 = 81;
    private static final int GFX_BOSS_NEGATIVE_1 = 92;
    private static final int GFX_BOSS_NEGATIVE_2 = 93;
    private static final int GFX_BOSS_NEGATIVE_3 = 94;
    private static final int GFX_BOSS_NEGATIVE_4 = 95;
    private static final int GFX_BOSS_NEGATIVE_5 = 96;
    private static final int GFX_BOSS_NEGATIVE_6 = 97;
    private static final int GFX_CRUMBLY_PLATFORM = 1;
    private static final int GFX_CRUSHER = 120;
    private static final int GFX_CRUSHER2 = 122;
    private static final int GFX_EXPLOSION_A = 45;
    private static final int GFX_EXPLOSION_B = 46;
    private static final int GFX_EXPLOSION_C = 47;
    private static final int GFX_EXPLOSION_D = 48;
    private static final int GFX_GREEN_BONUS_BACK = 11;
    private static final int GFX_GREEN_BONUS_FRONT = 52;
    private static final int GFX_GREEN_BONUS_GO_A = 53;
    private static final int GFX_GREEN_BONUS_GO_B = 54;
    private static final int GFX_HUD_LIVES = 35;
    private static final int GFX_HUD_RINGS = 34;
    private static final int GFX_LINK = 118;
    private static final int GFX_MENU_RING_A = 113;
    private static final int GFX_MENU_RING_B = 114;
    private static final int GFX_MENU_RING_C = 115;
    private static final int GFX_MENU_RING_D = 116;
    private static final int GFX_PLATFORM = 0;
    private static final int GFX_PLATFORM_CRUMBLING_A = 26;
    private static final int GFX_PLATFORM_CRUMBLING_B = 27;
    private static final int GFX_RESULTS_SHARD = 111;
    private static final int GFX_RESULTS_SHARD_BLACK = 124;
    private static final int GFX_RESULTS_SHARD_SPARKLE = 117;
    private static final int GFX_RESULTS_SILHOUETTE = 112;
    private static final int GFX_RING_A = 6;
    private static final int GFX_RING_B = 7;
    private static final int GFX_RING_C = 8;
    private static final int GFX_RING_D = 9;
    private static final int GFX_RING_VANISHING_A = 28;
    private static final int GFX_RING_VANISHING_B = 29;
    private static final int GFX_SHARD_1 = 88;
    private static final int GFX_SHARD_2 = 89;
    private static final int GFX_SHARD_3 = 90;
    private static final int GFX_SHARD_4 = 91;
    private static final int GFX_SMOKE_1 = 82;
    private static final int GFX_SMOKE_2 = 83;
    private static final int GFX_SMOKE_3 = 84;
    private static final int GFX_SMOKE_4 = 85;
    private static final int GFX_SMOKE_5 = 86;
    private static final int GFX_SMOKE_6 = 87;
    private static final int GFX_SONIC_CROUCHING = 37;
    private static final int GFX_SONIC_CROUCHING_FLIP = 38;
    private static final int GFX_SONIC_DEAD_FLIP = 50;
    private static final int GFX_SONIC_FALLING = 18;
    private static final int GFX_SONIC_FALLING_FLIP = 19;
    private static final int GFX_SONIC_HURT = 43;
    private static final int GFX_SONIC_HURT_FLIP = 44;
    private static final int GFX_SONIC_JUMPING = 16;
    private static final int GFX_SONIC_JUMPING_FLIP = 17;
    private static final int GFX_SONIC_SPINNING_A = 20;
    private static final int GFX_SONIC_SPINNING_A_FLIP = 21;
    private static final int GFX_SONIC_SPINNING_B = 22;
    private static final int GFX_SONIC_SPINNING_B_FLIP = 23;
    private static final int GFX_SONIC_SPINNING_C = 24;
    private static final int GFX_SONIC_SPINNING_C_FLIP = 25;
    private static final int GFX_SONIC_STANDING = 14;
    private static final int GFX_SONIC_STANDING_FLIP = 15;
    private static final int GFX_SPACE_SPIKEY_PLATFORM_A = 3;
    private static final int GFX_SPACE_SPIKEY_PLATFORM_B = 4;
    private static final int GFX_SPACE_SPIKEY_PLATFORM_C = 5;
    private static final int GFX_SPIKEY_PLATFORM = 2;
    private static final int GFX_SPINNER_POLE = 39;
    private static final int GFX_SPRING = 13;
    private static final int GFX_SPRING_EXTENDED = 10;
    private static final int GFX_SWISH_A = 30;
    private static final int GFX_SWISH_A_FLIP = 98;
    private static final int GFX_SWISH_B = 31;
    private static final int GFX_SWISH_B_FLIP = 99;
    private static final int GFX_SWISH_C = 32;
    private static final int GFX_SWISH_C_FLIP = 100;
    private static final int GFX_SWISH_D = 33;
    private static final int GFX_SWISH_D_FLIP = 101;
    private static final int GFX_ZONE_1_ENEMY_A = 58;
    private static final int GFX_ZONE_1_ENEMY_A_FLIP = 59;
    private static final int GFX_ZONE_1_ENEMY_B = 60;
    private static final int GFX_ZONE_1_ENEMY_B_FLIP = 61;
    private static final int GFX_ZONE_1_ENEMY_C = 109;
    private static final int GFX_ZONE_1_ENEMY_C_FLIP = 110;
    private static final int GFX_ZONE_2_ENEMY_A = 62;
    private static final int GFX_ZONE_2_ENEMY_A_FLIP = 63;
    private static final int GFX_ZONE_2_ENEMY_B = 64;
    private static final int GFX_ZONE_2_ENEMY_B_FLIP = 65;
    private static final int GFX_ZONE_3_ENEMY_A = 66;
    private static final int GFX_ZONE_3_ENEMY_A_FLIP = 67;
    private static final int GFX_ZONE_3_ENEMY_B = 68;
    private static final int GFX_ZONE_3_ENEMY_B_FLIP = 69;
    private static final int GFX_ZONE_4_ENEMY_A = 70;
    private static final int GFX_ZONE_4_ENEMY_A_FLIP = 102;
    private static final int GFX_ZONE_4_ENEMY_B = 71;
    private static final int GFX_ZONE_4_ENEMY_B_FLIP = 103;
    private static final int GFX_ZONE_5_ENEMY_A = 72;
    private static final int GFX_ZONE_5_ENEMY_A_FLIP = 74;
    private static final int GFX_ZONE_5_ENEMY_B = 73;
    private static final int GFX_ZONE_5_ENEMY_B_FLIP = 75;
    private static final int GFX_ZONE_6_ENEMY_A = 104;
    private static final int GFX_ZONE_6_ENEMY_B = 105;
    private static final int GFX_ZONE_6_ENEMY_C = 106;
    private static final int GFX_ZONE_6_ENEMY_D = 107;
    private static final int GFX_ZONE_6_ENEMY_E = 108;
    private static final int GFX_last = 9999;
    static final int HIGHSCORES = 4;
    private static final int INERTIA_BLUE_BONUS_FLING = -19;
    private static final int INERTIA_CURL_UP = -13;
    private static final int INERTIA_DYING_FLING = -10;
    private static final int INERTIA_ENEMY_BOUNCE_FLING = -9;
    private static final int INERTIA_LOSE_RINGS_FLING = -6;
    private static final int INERTIA_NEW_SUPER_JUMP = -6;
    private static final int INERTIA_NORMAL_JUMP = -9;
    private static final int INERTIA_NO_RING_FLING = -9;
    private static final int INERTIA_SPRING_FLING = -13;
    private static final int INERTIA_SUPERJUMP_END = 8;
    private static final int INERTIA_SUPERJUMP_START = -6;
    private static final int INERTIA_SUPER_JUMP = -10;
    private static final int INERTIA_TERMINAL_VELOCITY = 7;
    private static final int INERTIA_UNCURL = 3;
    static final int INFO = 16;
    static final int INTERRUPTED = 7;
    public static int INTERRUPTION_DELAY = 0;
    private static final int JUMP_BODGE = 4;
    static final int LANG = 3;
    static final int LAUNCH = 28;
    static final int LEVELDONE = 14;
    static final int LEVELFAILED = 6;
    static final int LEVELNAMEOFFSET = 236;
    private static final int LEVEL_GOTSHARD = 2;
    private static final int LEVEL_LOCKED = 0;
    private static final int LEVEL_OPEN = 1;
    static final int LINKCONFIRM = 34;
    public static final int LOOP_FALSE = 1;
    public static final int LOOP_TRUE = -1;
    static final int MAINMENU = 2;
    static final int MAXSPRITES = 65535 / 3/*354*/;
    public static long MIMIMUM_FRAME_TIME = 60L;
    static final int NOKIA_DEMO = 33;
    static final int OPTIONS = 15;
    static final int PAUSESCREEN = 13;
    public static final int PLATFORM_TRANS_X_MAGNITUDE = 0;
    static final int READY = 9;
    static final int RESULTS = 24;
    static int ResumeSound = 0;
    public static final int SFX_BLUE = 117;
    public static final int SFX_BONUS = 111;
    public static final int SFX_CREDITS = 109;
    public static final int SFX_CUTSCENE = 110;
    public static final int SFX_DASH = 118;
    public static final int SFX_END_LEV = 101;
    public static final int SFX_GAME_OVER = 102;
    public static final int SFX_GOT_RING = 113;
    public static final int SFX_KILL_ENEMY = 114;
    public static final int SFX_MENU_MOVE = 3;
    public static final int SFX_MENU_SELECT = 2;
    public static final int SFX_NONE = 0;
    public static final int SFX_OFF = -1;
    public static final int SFX_OUCH = 112;
    public static final int SFX_PLATFORM = 115;
    public static final int SFX_RESET_6630 = -99;
    public static final int SFX_RESUME_IF_POS = -3;
    public static final int SFX_SPRING = 116;
    public static final int SFX_START_LEV = 100;
    public static final int SFX_STOP_MUSIC_UNLESS_ALWAYS_ON = -2;
    public static final int SFX_TITLE = 1;
    public static final int SFX_ZONE_TUNE_1 = 103;
    public static final int SFX_ZONE_TUNE_2 = 104;
    public static final int SFX_ZONE_TUNE_3 = 105;
    public static final int SFX_ZONE_TUNE_4 = 106;
    public static final int SFX_ZONE_TUNE_5 = 107;
    public static final int SFX_ZONE_TUNE_6 = 108;
    public static final int SFX_last = 9999;
    public static final int SIXTY_SECONDS = 60000;
    private static final int SONICMODE_BEINGTRACTORED = 2;
    private static final int SONICMODE_CROUCHING = 3;
    private static final int SONICMODE_CROUCHING2 = 4;
    private static final int SONICMODE_DYING = 5;
    private static final int SONICMODE_JUMPING = 1;
    private static final int SONICMODE_STANDING = 0;
    private static final int SONICMODE_WON = 6;
    static final int SOUNDMENU = 17;
    public static final int SOUND_INIT = 0;
    public static final int SOUND_PLAY = 1;
    public static final int SOUND_PLAY_STORED = 4;
    public static final int SOUND_RESUME = 3;
    public static final int SOUND_STOP = 2;
    static final int SPLASH = -1;
    static final int SPLASH1 = 10;
    static final int SPLASH2 = 11;
    static final int SPLASH2B = 19;
    static final int SPLASH3 = 12;
    public static final int TIMER_MAX = 10;
    static final int UPSELL = 27;
    public static String VF_LINK = null;
    static final int ZONESELECT = 5;
    public static boolean demoEnded;
    public static String demoURL;
    public static boolean doReset;
    public static int flashTimer;
    public static Game_Draw frame;
    private static boolean isPainting;
    private static int langSelected;
    static int language;
    public static String[] localText;
    private static boolean m_bInited;
    //private static boolean HigherScale;
    //private static boolean Higher = true;
    public volatile static boolean multip = false;
    public static int backPressCount = 0;
    public static long pauseStartTime;
    public static int touchState;
    public static String vodaDemo;
    static int volume;
    private static final byte[] zonemap1;
    private static final byte[] zonemap2;
    private static final byte[] zonemap3;
    private static final byte[] zonemap4;
    private static final byte[] zonemap5;
    private static final byte[] zonemap6;
    private DeviceImage BlockPic;
    private DeviceImage CSPic1;
    private DeviceImage CSPic2;
    private DeviceImage CSPic3;
    private Bitmap DevPic;
    private Bitmap RollPic;
    private DeviceSound DeviceSound;
    private DeviceImage EdgePic;
    private DeviceImage[] ExtrasPic;
    int FlagVal;
    public static int HEIGHT;
    public int LoopModeWanted;
    public int MenuTimer;
    private DeviceImage PausePic;
    int RestartMins;
    int RestartPoint;
    int RestartPointx;
    int RestartPointy;
    int RestartSecs;
    int RestartTicks;
    int ResumeMode;
    int ResumeModeWanted;
    int ResumeSK1;
    int ResumeSK2;
    private boolean SkipRotation;
    int SoundAvailable;
    public int SoundOnOff;
    private DeviceImage SpritePic;
    private DeviceImage StripPic;
    int TOTMAPBINSIZE;
    int TextOff;
    private DeviceImage TilesPic;
    public int TimeTicks;
    public int VibraOnOff;
    public static int WIDTH;
    int act;
    int addoverlay;
    int addoverlay2;
    int amf;
    int amh;
    int amx;
    int amy;
    int ao;
    int b;
    int bd;
    int bdun;
    byte[] bgrlayer;
    int bonusat;
    String bonustext;
    int bonustime;
    int bored;
    int bossbob;
    int bosshealth;
    int bosshurt;
    int bosslistptr;
    int bossmode;
    int bosstime;
    int boxh;
    int ch;
    int ch2;
    int chain1x;
    int chain1y;
    int chain2x;
    int chain2y;
    int chainfall;
    int chainfallg;
    int chainlen;
    int chainrot1;
    int chainvel;
    int choice;
    int cshigh;
    public int[] cslist;
    int currentmap;
    long currenttime;
    int d;
    public String demoMode;
    private DeviceImage demoSplashImage;
    private DeviceImage demoTitleImage;
    long dif;
    int dp;
    int drawY;
    private Rect dst;
    private Rect dst_m;
    int dx;
    int dy;
    int firstjump;
    private DeviceImage fontImage;
    private float fps;
    int gapchar;
    int gothiscore;
    int gzone;
    int h;
    int he;
    int hsx;
    int hsy;
    int hurt;
    int ilang;
    int inertia;
    int initPosY;
    int invulnerable;
    int lastadded;
    int lastxprint;
    DataInputStream ldin;
    int levelover;
    int levelovertime;
    int lineshown;
    int lives;
    int livesshown;
    int mWanted;
    private int m_PayingType;
    private Bitmap m_TitleBg;
    private Bitmap m_ZoneSelectBg;
    private Bitmap m_imgCSEnding;
    public int m_nPayPoint;
    private float m_scale;
    int maxlines;
    int maxscrolly;
    int maxy;
    private int mbx;
    private int mby;
    int mins;
    int mo;
    int modefrom;
    public String multiLang;
    int mx;
    int my;
    int mye;
    int n;
    int n2;
    int needswish;
    int numstrings;
    int o;
    int olay;
    int ovx;
    int ovx2;
    int ovy;
    int ovy2;
    int p;
    int panhigh;
    char[] para;
    int paralen;
    int perc;
    int r;
    private Random random;
    int realbossx;
    int realbossy;
    byte[] realmap;
    int res;
    String returnString;
    int rings;
    int ringsshown;
    int rp;
    private float scale_x;
    private float scale_y;
    int score;
    int scrolly;
    int scrollywanted;
    int secs;
    int sect;
    int sf;
    int si;
    int sk1;
    int sk2;
    int skflip;
    private DeviceImage softkey;
    float sonicacc;
    int sonicdir;
    int sonicmode;
    int sonicoffset;
    float sonicx;
    float sonicy;
    int spic;
    private Rect src;
    private Rect src_bg;
    int srot;
    private DeviceImage staticBG;
    int stringmax;
    int stringptr;
    int superjump;
    public String switchButtons;
    int t;
    int tFlag;
    int telemax;
    int teletype;
    int temp;
    int temp1;
    int temp2;
    int temp3;
    int textend;
    int textheading;
    int ticks;
    int timebonus;
    int toosoon;
    int tractortime;
    int tut;
    int tut2done;
    int tutdone;
    int type;
    public String upsell;
    int w;
    int wonattatch;
    int wonattatchx;
    int x;
    int yoff;
    int zm;
    int zone;
    byte[] zonemapptr;
    public static int demoSecs = 0;
    public static int demoLastSecs = 0;
    public static int LoopMode = 0;
    private static final int GFX_SONIC_DEAD = 49;
    private static final int GFX_SPRING_SQUASHED = 51;
    private static final int GFX_BLUE_BONUS_GO_B = 57;
    private static final int GFX_BLUE_BONUS_FRONT = 55;
    static int[] cheat = {GFX_SONIC_DEAD, GFX_SPRING_SQUASHED, GFX_BLUE_BONUS_GO_B, GFX_BLUE_BONUS_FRONT, GFX_SPRING_SQUASHED, GFX_SONIC_DEAD};
    static int[] cheat2 = {GFX_SONIC_DEAD, GFX_BLUE_BONUS_FRONT, GFX_BLUE_BONUS_GO_B, GFX_SPRING_SQUASHED, GFX_BLUE_BONUS_FRONT, GFX_SONIC_DEAD};
    public static int soundtime = 0;
    public static byte[] tuneflags = {2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1};
    private byte[] http_data = null;
    public byte[] menu = new byte[100];
    public int key_up = 0;
    public int key_down = 0;
    public int key_left = 0;
    public int key_right = 0;
    public int key_fire = 0;
    public int key_exit = 0;
    public int key_func1 = 0;
    public int key_func2 = 0;
    public int key_back = 0;
    public int key_star = 0;
    public int key_hash = 0;
    public int key_1 = 0;
    public int key_2 = 0;
    public int key_3 = 0;
    public int key_others = 0;
    public boolean KEY_RELEASED = true;
    int lastpressed = 0;
    int keypressed = 0;
    public boolean PreviousMenu = false;
    public boolean PreviousLevel = false;
    public int PreviousZone = 0;
    public int FirstFrame = 1;
    public int TouchpadVisibleOnOff = 0;
    public byte[] highscores = new byte[220];
    public byte[] highscoresbuffer = new byte[GFX_SPINNER_POLE];
    public int vibrawanted = 0;
    private DeviceImage TitlePic = new DeviceImage(this.m_res, "tiltle");
    private DeviceImage DiamondPic = new DeviceImage(196616);
    private DeviceImage PubPic = new DeviceImage(196620);
    private DeviceImage DevPic2 = TitlePic;
    private DeviceImage MenuPic = TitlePic;
    private DeviceImage ZsPic = new DeviceImage(196622);
    private DeviceImage ZrdPic = new DeviceImage(196621);
    private int[][] ZrdPixel = {new int[]{0, 0, 21, 32, 217}, new int[]{21, 0, 17, 32, 229}, new int[]{GFX_SONIC_CROUCHING_FLIP, 0, 21, 32, 237}};
    public int MainMenuPtr = 0;
    private DeviceImage[] BlockPics = new DeviceImage[21];
    private DeviceImage[] CSPics = new DeviceImage[27];
    private DeviceImage[] StripPics = new DeviceImage[21];
    private DeviceImage[] TilesPics = new DeviceImage[21];
    private DeviceImage[] EdgePics = new DeviceImage[21];
    byte[] enemyx = new byte[MAXSPRITES];
    int[] enemyy = new int[MAXSPRITES];
    byte[] enemytype = new byte[MAXSPRITES + 1 /*355*/];
    byte[] enemydir = new byte[MAXSPRITES];
    int[] enemymove = new int[MAXSPRITES];
    int[] enemydata = new int[MAXSPRITES];
    int cheatptr = 0;
    int cheatptr2 = 0;
    int[] cheatactive = new int[6];
    int[] roomsize = new int[21];
    int[] roomindex = new int[21];
    String[] text = new String[336];
    boolean StartSoundPlayed = true;
    private DeviceImage brandImage = new DeviceImage();
    private int nImageNumber = 0;
    private long displayTime = System.currentTimeMillis();
    private boolean rotating = true;
    boolean demoStarted = false;
    byte appreset = 0;
    int skupdate = 0;
    int splashdone = 0;
    int ink = 0;
    int digatalalti = 0;
    public float sensor_x = 0.0f;
    public float sensor_y = 0.0f;
    int ignorekeys = 0;
    int ssely = 0;
    int lselx = 0;
    int lsely = 0;
    int[] sintab = {0, GFX_SWISH_A_FLIP, 181, 237, 256, 237, 181, GFX_SWISH_A_FLIP, 0, -98, -181, -237, -256, -237, -181, -98, 0, GFX_SWISH_A_FLIP, 181, 237};
    public int musicwanted = 0;
    private int SFX_MAX = 8;
    private int[] sfxwanted = new int[this.SFX_MAX];
    int[] nmetop = new int[6];
    int[] nmebot = {26, 21, 24, 26, 21, 24};
    int[] nmehalfwid = {7, 9, 11, 7, 8, 7};
    int[] bosslist = {0, 0, 10, 0, 0, 0, 4, GFX_SONIC_CROUCHING_FLIP, 2, 0, -4, 0, 20, 3, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 4, 1, 4, 0, GFX_ANTENNA_OFF, 5, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 6, 1, -4, 0, 20, 7, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 2, 1, 0, 4, GFX_SONIC_DEAD_FLIP, 9, 2, -4, 0, 20, 10, 2, 4, 0, GFX_ANTENNA_OFF, 11, 2, -4, 0, GFX_ANTENNA_OFF, 10, 2, -2, 4, GFX_ANTENNA_OFF, 13, 3, 2, 0, GFX_BOSS_5, 14, 3, COLL_NME_LEFT, 6, 20, 15, 3, 2, 0, GFX_BOSS_5, 16, 3, COLL_NME_LEFT, -6, 10, 17, 3, 0, 0, 160, 18, 4, COLL_NME_LEFT, -6, 10, 13, 3, 0, 4, GFX_SONIC_CROUCHING_FLIP, 20, 0, 4, 0, 20, 21, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 22, 5, -4, 0, GFX_ANTENNA_OFF, 23, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 24, 5, 4, 0, 20, GFX_SONIC_SPINNING_C_FLIP, 0, 0, 0, GFX_SONIC_DEAD_FLIP, 26, 5, -4, 0, 20, 27, 0, 0, 8, 16, 28, 0, 0, -4, 32, 29, 0, 16, 0, 10, 30, 0, 0, 8, 16, 31, 0, 0, -4, 32, 32, 0, -16, 0, 10, 33, 0, 16, 0, 5, 34, 0, 0, 8, 16, GFX_HUD_LIVES, 0, 0, -4, 32, GFX_ALTIMETER_POINTER, 0, 4, 0, 20, GFX_SONIC_CROUCHING, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SONIC_CROUCHING_FLIP, 5, -4, 0, GFX_ANTENNA_OFF, GFX_SPINNER_POLE, 0, 4, 0, 20, GFX_ANTENNA_OFF, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_ANTENNA_ON, 5, -4, 0, 20, GFX_ANTENNA_BASE, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SONIC_HURT, 5, 4, 0, GFX_ANTENNA_OFF, GFX_SONIC_HURT_FLIP, 0, 0, 8, 16, GFX_EXPLOSION_A, 0, 0, -4, 32, GFX_EXPLOSION_B, 0, -16, 0, 10, GFX_EXPLOSION_C, 0, 0, 8, 16, GFX_EXPLOSION_D, 0, 0, -4, 32, GFX_SONIC_DEAD, 0, 16, 0, 10, GFX_SONIC_DEAD_FLIP, 0, -16, 0, 5, GFX_SPRING_SQUASHED, 0, 0, 8, 16, GFX_GREEN_BONUS_FRONT, 0, 0, -4, 32, GFX_GREEN_BONUS_GO_A, 0, -4, 0, 20, GFX_GREEN_BONUS_GO_B, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_BLUE_BONUS_FRONT, 5, 4, 0, GFX_ANTENNA_OFF, GFX_BLUE_BONUS_GO_A, 0, -4, 0, 20, GFX_BLUE_BONUS_GO_B, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_ZONE_1_ENEMY_A, 5, -4, 0, 20, GFX_ZONE_1_ENEMY_A_FLIP, 0, 4, 0, GFX_ANTENNA_OFF, GFX_ZONE_1_ENEMY_B, 0, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_ZONE_1_ENEMY_B_FLIP, 5, -4, 0, 20, GFX_ZONE_2_ENEMY_A, 0, 0, 8, 16, GFX_ZONE_2_ENEMY_A_FLIP, 0, 0, -4, 32, GFX_ZONE_2_ENEMY_B, 0, 16, 0, 5, GFX_ZONE_2_ENEMY_B_FLIP, 0, -16, 0, 10, GFX_ZONE_3_ENEMY_A, 0, 0, 8, 16, GFX_ZONE_3_ENEMY_A_FLIP, 0, 0, -4, 32, GFX_ZONE_3_ENEMY_B, 0, 16, 0, 10, GFX_ZONE_3_ENEMY_B_FLIP, 0, 0, 8, 16, GFX_ZONE_4_ENEMY_A, 0, 0, -4, 32, 21, 0, 0, 4, GFX_EXPLOSION_A, GFX_ZONE_5_ENEMY_A, 6, -2, 0, GFX_ANTENNA_OFF, GFX_ZONE_5_ENEMY_B, 6, 2, 0, GFX_BOSS_5, GFX_ZONE_5_ENEMY_A_FLIP, 6, -2, 0, GFX_BOSS_5, GFX_ZONE_5_ENEMY_B_FLIP, 6, 0, 2, GFX_ANTENNA_OFF, GFX_BOSS_1, 6, 2, 0, GFX_BOSS_5, GFX_BOSS_2, 6, 0, -2, GFX_ANTENNA_OFF, GFX_BOSS_3, 6, -2, 0, GFX_BOSS_5, GFX_ZONE_5_ENEMY_B, 6, 0, 4, GFX_SONIC_DEAD_FLIP, GFX_BOSS_5, 7, 2, 0, GFX_ANTENNA_OFF, GFX_BOSS_6, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SMOKE_1, 7, COLL_NME_LEFT, 4, 20, GFX_SMOKE_2, 7, 0, -2, GFX_ANTENNA_OFF, GFX_SMOKE_3, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SMOKE_4, 7, 8, 4, 20, GFX_SMOKE_5, 7, 0, -2, GFX_ANTENNA_OFF, GFX_SMOKE_6, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SHARD_1, 7, -4, -2, 20, GFX_SHARD_2, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_SHARD_3, 7, -4, 2, 20, GFX_SHARD_4, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_BOSS_NEGATIVE_1, 7, 4, -2, 20, GFX_BOSS_NEGATIVE_2, 7, 0, 0, GFX_SONIC_DEAD_FLIP, GFX_BOSS_NEGATIVE_3, 7, 4, 2, 20, GFX_BOSS_6, 7};
    int[] bossstart = {1, 12, 8, 19, GFX_ZONE_4_ENEMY_B, GFX_BOSS_4, GFX_BOSS_4};
    public String version = null;
    public String upsellURL = "https://furrican.github.io/sonic-jump-anniversary";
    int incLang = 0;
    int incUpsell = 0;
    int incDemo = 0;
    int activaterequest = 0;
    int jarexists = 0;
    int delayedrequest = 2;
    int csnum = 1;
    byte[] picflags = {0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 5, 6, 7, 3, 3, 3, 1};
    int slidex = 0;
    int slidey = 0;
    int slidexoff = 0;
    int slideyoff = 0;
    int[] SKptrs = {21, 9, 11, 10, 12, 14, 13, 7, 191, 193, 192};
    public Bitmap[] touchpad = new Bitmap[8];
    private boolean m_bNeedTitleBg = false;
    private int[] m_GameColor = {-16645499, -12613121, -8108972, -10779204, -16622890, -16645630, -16645630};
    private long lastTick = -1;
    int mtry = 0;
    private Thread thread = null;
    private boolean running = false;
    public boolean b_Exit = false;
    int[] props = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 5, 2, 4, 4, 3, 4, 4, 5, 4, 4, 4, 4, 4, 5, 8, 4, 4, 4, 4, 3, 3, 4, 4, 3, 4, 4, 3, 5, 4, 4, 4, 5, 4, 3, 3, 4, 5, 5, 3, 3, 4, 6, 5, 6, 2, 1, 5, 1, 2, 2, 2, 1};
    int[] coff = new int[GFX_BLUE_BONUS_FRONT];
    String[] stringlist = new String[GFX_SONIC_DEAD_FLIP];
    int ringval = 0;
    int lefttodraw = 0;
    int remaining = 0;
    int spark = 0;
    long frametime = 0;
    boolean m_cheat = false;
    int menuFix = 0;
    String szHalfWord = "";
    int nMultiplier = 0;
    int bonusunlocked = 0;
    int totalZonesUnlocked = 0;
    int lasthiscore = -1;
    private String SAVEFILE = "sj_save.dat";
    boolean e700Sound = false;
    Rect b_right = new Rect(0, 496, 128, 840);
    Rect b_left = new Rect(232, 496, 360, 840);
    Rect b_mid1 = new Rect(128, 496, 232, 840);
    Rect b_mid2 = new Rect(0, 64, 360, 840);
    private boolean m_bPaying = false;
    private List<IniCutscene> iniCutscenes = new ArrayList<>();
    private int currentIniCutscene = 0;
    private int currentLine = 0;
    private Bitmap[] customBack = new Bitmap[21];
    private DeviceImage[] customFront = new DeviceImage[21];

    static {
        byte[] bArr = new byte[GFX_BOSS_1];
        bArr[4] = 1;
        bArr[5] = 1;
        bArr[6] = 1;
        bArr[9] = 6;
        bArr[12] = 1;
        bArr[13] = 7;
        bArr[14] = 1;
        bArr[20] = 1;
        bArr[21] = 1;
        bArr[22] = 5;
        bArr[28] = 1;
        bArr[29] = 1;
        bArr[30] = 1;
        bArr[34] = 6;
        bArr[GFX_ALTIMETER_POINTER] = 5;
        bArr[GFX_SONIC_CROUCHING] = 1;
        bArr[GFX_SONIC_CROUCHING_FLIP] = 7;
        bArr[GFX_SONIC_HURT_FLIP] = 1;
        bArr[GFX_EXPLOSION_A] = 1;
        bArr[GFX_EXPLOSION_B] = 5;
        bArr[GFX_EXPLOSION_D] = 6;
        bArr[GFX_GREEN_BONUS_FRONT] = 7;
        bArr[GFX_GREEN_BONUS_GO_A] = 5;
        bArr[GFX_GREEN_BONUS_GO_B] = 1;
        bArr[GFX_ZONE_1_ENEMY_B] = 1;
        bArr[GFX_ZONE_1_ENEMY_B_FLIP] = 1;
        bArr[GFX_ZONE_2_ENEMY_A] = 1;
        bArr[GFX_ZONE_2_ENEMY_B_FLIP] = 6;
        bArr[GFX_ZONE_3_ENEMY_B] = 1;
        bArr[GFX_ZONE_3_ENEMY_B_FLIP] = 7;
        bArr[GFX_ZONE_4_ENEMY_A] = 1;
        zonemap1 = bArr;
        byte[] bArr2 = new byte[GFX_SMOKE_3];
        bArr2[2] = 5;
        bArr2[3] = 1;
        bArr2[4] = 1;
        bArr2[6] = 6;
        bArr2[10] = 1;
        bArr2[13] = 7;
        bArr2[16] = 1;
        bArr2[19] = 4;
        bArr2[20] = 2;
        bArr2[21] = 3;
        bArr2[26] = 1;
        bArr2[28] = 1;
        bArr2[34] = 7;
        bArr2[GFX_HUD_LIVES] = 1;
        bArr2[GFX_SONIC_CROUCHING] = 5;
        bArr2[GFX_SONIC_CROUCHING_FLIP] = 1;
        bArr2[GFX_ANTENNA_ON] = 6;
        bArr2[GFX_ANTENNA_BASE] = 4;
        bArr2[GFX_SONIC_HURT] = 2;
        bArr2[GFX_SONIC_HURT_FLIP] = 7;
        bArr2[GFX_EXPLOSION_B] = 1;
        bArr2[GFX_SONIC_DEAD] = 1;
        bArr2[GFX_GREEN_BONUS_FRONT] = 3;
        bArr2[GFX_GREEN_BONUS_GO_B] = 7;
        bArr2[GFX_BLUE_BONUS_GO_B] = 7;
        bArr2[GFX_ZONE_1_ENEMY_A] = 1;
        bArr2[GFX_ZONE_1_ENEMY_A_FLIP] = 4;
        bArr2[GFX_ZONE_1_ENEMY_B] = 1;
        bArr2[GFX_ZONE_3_ENEMY_A] = 5;
        bArr2[GFX_ZONE_3_ENEMY_A_FLIP] = 1;
        bArr2[GFX_ZONE_3_ENEMY_B] = 1;
        bArr2[GFX_ZONE_4_ENEMY_A] = 6;
        bArr2[GFX_ZONE_5_ENEMY_A_FLIP] = 1;
        bArr2[GFX_BOSS_2] = 7;
        bArr2[GFX_BOSS_5] = 1;
        bArr2[GFX_SMOKE_2] = 4;
        zonemap2 = bArr2;
        zonemap3 = new byte[]{0, 0, 3, 0, 3, 6, 6, 0, 2, 4, 4, 0, 2, 5, 5, 0, 0, 4, 4, 0, 3, 6, 6, 0, 2, 7, 7, 0, 1, 2, 1, 0, 0, 0, 3, 0, 3, 6, 6, 0, 2, 4, 4, 0, 2, 5, 5, 0, 0, 4, 4};
        zonemap4 = new byte[]{0, 0, 6, 0, 1, 7, 1, 0, 0, 4, 0, 0, 1, 5, 1, 0, 2, 0, 4, 0, 3, 1, 5, 0, 0, 6, 0, 0, 1, 1, 7, 0, 6, 0, 2, 0, 1, 7, 3, 0, 0, 0, 4, 0, 1, 1, 5, 0, 0, 2, 6, 0, 7, 3, 1, 0, 0, 0, 6, 0, 1, 7, 1, 0, 0, 4, 0, 0, 1, 5, 1, 0, 2, 0, 4};
        zonemap5 = new byte[]{0, 0, 7, 0, 1, 5, 4, 0, 2, 7, 2, 0, 3, 1, 3, 0, 6, 4, 5, 0, 5, 7, 6, 0, 0, 2, 7, 0, 1, 3, 2, 0, 0, 0, 7, 0, 1, 5, 4, 0, 2, 7, 2, 0, 3, 1, 3, 0, 6, 4, 5};
        zonemap6 = new byte[]{1, 5, 7, 5, 2, 1, 4, 5, 3, 7, 1, 7, 1, 5, 7, 6, 1, 7, 1, 7, 6, 5, 2, 5, 5, 7, 3, 5, 1, 4, 5, 1, 1, 5, 7, 5, 2, 1, 4, 5, 3, 7, 1, 7, 1, 5, 7, 6, 1, 7, 1, 7};
        demoURL = null;
        vodaDemo = null;
        VF_LINK = null;
        pauseStartTime = 0L;
        m_bInited = false;
        //MIMIMUM_FRAME_TIME = 60L;
        INTERRUPTION_DELAY = 0;
        doReset = false;
        flashTimer = 10;
        touchState = 0;
        volume = 0;
    }

    void processSK1(int sk1) {
        if (this.skflip != 0) {
            if (this.sk2 == 2) {
                this.key_back = 1;
            }
            if (this.sk2 == 4) {
                this.LoopModeWanted = 13;
            }
            if (this.sk2 == 5) {
                while (this.cslist[(this.csnum * 5) + 4] == 0) {
                    this.csnum++;
                }
                this.key_fire = 1;
                this.toosoon = 0;
                this.telemax = 0;
            }
            if (this.sk2 == 6) {
                this.LoopModeWanted = 1;
            }
            if (this.sk2 == 7) {
                this.key_exit = 1;
            }
            if (this.sk2 == 10) {
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        this.key_fire = 1;
    }

    void processSK2(int sk2) {
        if (this.skflip != 0) {
            this.key_fire = 1;
            return;
        }
        if (sk2 == 2) {
            this.key_back = 1;
        }
        if (sk2 == 4) {
            this.LoopModeWanted = 13;
        }
        if (sk2 == 5) {
            while (this.cslist[(this.csnum * 5) + 4] == 0) {
                this.csnum++;
            }
            LoopMode = 23;
            int p = this.csnum * 5;
            if (this.cslist[p + 4] == -1) {
                this.LoopModeWanted = 2;
            }
            if (this.cslist[p + 4] == -2) {
                this.LoopModeWanted = 9;
            }
            if (this.cslist[p + 4] == -3) {
                this.LoopModeWanted = 5;
            }
            if (this.cslist[p + 4] == -6) {
                this.csnum = 0;
                this.LoopModeWanted = 20;
            }
        }
        if (sk2 == 6) {
            this.LoopModeWanted = 1;
        }
        if (sk2 == 7) {
            this.key_exit = 1;
        }
        if (sk2 == 10) {
            this.LoopModeWanted = 2;
        }
    }

    protected void clearkeys() {
        this.choice = 0;
        this.key_func1 = 0;
        this.key_func2 = 0;
        this.key_up = 0;
        this.key_left = 0;
        this.key_fire = 0;
        this.key_right = 0;
        this.key_down = 0;
        this.key_back = 0;
        this.key_star = 0;
        this.key_hash = 0;
        this.key_1 = 0;
        this.key_2 = 0;
        this.key_3 = 0;
    }

    protected void reset() {
        if (LoopMode != 10 && LoopMode != 11 && LoopMode != 19 && LoopMode != 0 && !this.m_bPaying && LoopMode != 7) {
            this.ResumeMode = LoopMode;
            this.ResumeModeWanted = this.LoopModeWanted;
            this.ResumeSK1 = this.sk1;
            this.ResumeSK2 = this.sk2;
            ResumeSound = DeviceSound.getLastSound();
            processSound(2, 0, 0);
            this.musicwanted = -1;
            DeviceSound.releaseAll();
            this.LoopModeWanted = 7;
            LoopMode = 7;
            this.FirstFrame = 1;
            clearkeys();
            pauseStartTime = System.currentTimeMillis();
        }
    }

    public Game_Draw() {
        this.LoopModeWanted = 3;
        this.SoundOnOff = 1;
        this.VibraOnOff = 0;
        this.DevPic = null;
        this.SkipRotation = false;
        this.SoundAvailable = 2;
        this.multiLang = null;
        this.upsell = null;
        this.switchButtons = null;
        this.demoMode = null;
        this.skflip = 0;
        try {
            processSound(0, 0, 0);
        } catch (Exception e) {
            this.SoundAvailable = 0;
        }
        DeviceImage.setResources(this.m_res);
        ResourceMaster.setResources(this.m_res);
        Standard.setResources(this.m_res);
        if (this.multiLang == null) {
            this.multiLang = "1";
        }
        if (this.upsell == null) {
            this.upsell = "2";
        }
        if (this.switchButtons == null) {
            this.switchButtons = "0";
        }
        if (this.demoMode == null) {
            this.demoMode = "0";
        }
        if (vodaDemo == null) {
            vodaDemo = "0";
        }
        frame = this;
        this.fontImage = new DeviceImage(this.m_res, "font");
        this.softkey = new DeviceImage(this.m_res, "soft_key");
        int n = 0;
        for (int x = 0; x < GFX_BLUE_BONUS_FRONT; x++) {
            this.coff[x] = n;
            n += this.props[x + 10];
        }
        try {
            this.brandImage.LoadPic("brand" + this.nImageNumber);
        } catch (Exception e2) {
            this.SkipRotation = true;
        }
        this.random = new Random();
        this.highscores[0] = 0;
        this.highscores[1] = 1;
        this.highscores[3] = 0;
        this.highscores[4] = 1;
        this.highscores[5] = 0;
        setlock(1);
        resetscores();
        setupCS();
        if (this.switchButtons.equals("1")) {
            this.skflip = 1;
        }
        LoadGame();
        //SaveGame();
        this.SoundOnOff = this.highscores[0];
        this.VibraOnOff = this.highscores[1];
        language = this.highscores[3];
        if (language != 0) {
            initLanguage(language);
            if (!this.SkipRotation) {
                this.LoopModeWanted = 29;
            } else {
                this.LoopModeWanted = 10;
            }
        } else if (this.multiLang.equals("1")) {
            //initLanguage(1);
            String jlanguage = Locale.getDefault().getLanguage();
        if (jlanguage.equals("fr")) {
            initLanguage(2);
        } else if (jlanguage.equals("es")) {
            initLanguage(5);
        } else if (jlanguage.equals("pt")) {
            initLanguage(Locale.getDefault().getCountry().equals("BR") ? 7 : 6);
        } else if (jlanguage.equals("de")) {
            initLanguage(4);
        } else if (jlanguage.equals("it")) {
            initLanguage(3);
        } else if (jlanguage.equals("ja")) {
            initLanguage(8);
        } else if (jlanguage.equals("zh")) {
            initLanguage(9);
        }  else if (jlanguage.equals("en")) {
            initLanguage(1);
        } else {
            initLanguage(1);
        }
        
        } else {
            initText("lang");
        }
        try {
            PauseGameTouch = true;
new Thread(new Runnable() {
    @Override
    public void run() {
            try {
            DevPic = new DeviceImage("bg0").getBitmap();
            Thread.sleep(1000);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll0").getBitmap();
            DrawFirstSplash2(RollPic);
            aIntro = true;
            Thread.sleep(200);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll1").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(800);
            //PauseGameTouch = false;
            Thread.sleep(200);
            String[] imagens = {"roll2", "roll3", "roll4", "roll5", "roll6"};
for (int i = 0; i < 5; i++) { // O loop vai se repetir duas vezes
    for (String imagem : imagens) {
        DevPic = new DeviceImage("bg1").getBitmap();
        DrawFirstSplash2(DevPic);
        RollPic = new DeviceImage(imagem).getBitmap();
        DrawFirstSplash2(RollPic);
        Thread.sleep(100);
    }
}
//PauseGameTouch = false;
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll7").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(100);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll8").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(100);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll9").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(100);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            PauseGameTouch = false;
            RollPic = new DeviceImage("roll10").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(100);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll11").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(500);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll12").getBitmap();
            DrawFirstSplash2(RollPic);
            Thread.sleep(1500);
            DevPic = new DeviceImage("bg1").getBitmap();
            DrawFirstSplash2(DevPic);
            RollPic = new DeviceImage("roll13").getBitmap();
            DrawFirstSplash2(RollPic);
            //Thread.sleep(1500);
            /*DevPic = BitmapFactory.decodeResource(m_res, R.drawable.class.getField("cp").getInt(null), m_Option);
            Thread.sleep(2000);
            DevPic = new DeviceImage(196620).getBitmap();
            PauseGameTouch = false;
            Thread.sleep(2000);
            DevPic = new DeviceImage(196615).getBitmap();*/
        } catch (Exception e3) {
            e3.printStackTrace();
        }
    }
}).start();
        } catch (Exception e3) {
            e3.printStackTrace();
        }
        this.LoopModeWanted = 10;
        new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    Thread.sleep(1800);
    } catch (Exception e) {
    }
        playSound("intro", false);
            }
            }).start();
    }
    
    public void ProcessEvent() {
    setsoftkeys(1, 2);
    DoMenu(36, 1);
    if (res == 0) {
    event = true;
    if (eventrings2 != 0 && eventbluehoops2 != 0 && !ReadEventFile().equals("")) {
    shadow = false;
    tails = false;
    sonic = false;
    if (ReadEventFile().contains("sonic")) {
    sonic = true;
    shadow = false;
    tails = false;
    } else if (ReadEventFile().contains("shadow")) {
    shadow = true;
    sonic = false;
    tails = false;
    } else if (ReadEventFile().contains("tails")) {
    tails = true;
    shadow = false;
    tails = false;
    } else {
    sonic = true;
    shadow = false;
    tails = false;
    }
    this.SpritePic = new DeviceImage(ReadEventFile());
    this.LoopModeWanted = 2;
    } else {
    event = false;
    this.LoopModeWanted = 2;
    }
    }
        if (this.key_back == 1) {
                event = false;
                this.LoopModeWanted = 2;
                return;
            }
    }

    public void DrawEvent() {
    try {
    List<Event> events = IniParser.parseFromUrl(Event + "Event.ini");
    if (events != null) {
    for (Event e : events) {
    if (e.eventNumber == 1) {
    if (!e.textures.equals("") && !e.unlockable.equals("")) {
    if (ReadEventFile().equals("")) {
    eventrings2 = 0;
    eventbluehoops2 = 0;
    }
        eventname = e.name;
        eventtextures = e.textures;
        eventlevel = e.level;
        eventlevel2 = e.act;
        eventrings = e.rings;
        eventbluehoops = e.bluehoops;
        if (eventbluehoops == 0) eventbluehoops2 = 1;
        eventcharacter = e.character;
        eventunb = e.unlockable;
        SaveEventFile();
        }
    Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(360);
        if (this.G != null) {
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        }
        if (getBitmapFromURL(Event + "Event" + eventtextures + ".png") != null) {
        this.G.wg_drawImage(getBitmapFromURL(Event + "Event" + eventtextures + ".png"), this.GameWidth >> 1, this.GameHeight >> 1, 8704);
        Tad(e.name, Color_White, false);
        Gap();
        } else {
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(160);
        Tad(localText[225], Color_White, false);
            }
            }
            }
            } else {
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(160);
        Tad(localText[225], Color_White, false);
        Gap();
            }
} catch (IOException ex) {
    ex.printStackTrace();
}
    }

    public static Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            if (connection == null) {
            return null;
            }
            return BitmapFactory.decodeStream(input);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public void surfaceCreated(SurfaceHolder holder) {
        this.thread = new Thread(this);
        this.thread.start();
    }

    private void init() {
        loadCutscenesIni(Shell.sInstance, "shadow_campaign/cutscenes_" + CurrentL + ".ini");
        loadCustomBackIni();
        /*while (Higher) {
        }*/
        this.incLang = 1;
        if (!m_bInited) {
            m_bInited = true;
            this.WIDTH = getWidth();
            //this.HEIGHT = getHeight();
            /*if (HigherScale) {
            this.HEIGHT = getHeight();
            } else {*/
            this.HEIGHT = (getWidth() * 16) / 9;
            //}
            this.scale_x = ((float) this.GameWidth) / ((float) this.WIDTH);
            this.scale_y = ((float) this.GameHeight) / ((float) this.HEIGHT);
            /*this.scale_x = 1f;
            this.scale_y = 1f;*/
            this.src = new Rect(0, 0, this.GameWidth, this.GameHeight);
            this.dst_m = new Rect(0, 0, this.WIDTH, this.HEIGHT);
            /*if (this.scale_x > this.scale_y) {
                this.dst = new Rect(0, (int) ((((float) this.HEIGHT) - (((float) this.GameHeight) / this.scale_x)) / 2.0f), this.WIDTH, (int) ((((float) this.HEIGHT) + (((float) this.GameHeight) / this.scale_x)) / 2.0f));
            } else {
                this.dst = new Rect((int) ((((float) this.WIDTH) - (((float) this.GameWidth) / this.scale_y)) / 2.0f), 0, (int) ((((float) this.WIDTH) + (((float) this.GameWidth) / this.scale_y)) / 2.0f), this.HEIGHT);
                //this.dst = new Rect(0, 0, this.WIDTH, this.HEIGHT);
            }*/
            this.dst = new Rect(0, 0, this.WIDTH, this.HEIGHT);
            this.G = new Graphics(this.GameWidth, this.GameHeight, 0, 0);
            this.G.wg_setFontSize(24);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        this.running = false;
        try {
            Thread.sleep(500L);
        } catch (Exception e) {
        }
        this.thread = null;
    }

    private void flash() {
        try {
            if (this.m_Paint == null) {
                this.m_Paint = new Paint(3);
            }
            this.m_Canvas = this.holder.lockCanvas();
            if (this.m_Canvas != null) {
                if (LoopMode == 10) {
                    this.m_Paint.setColor(-1);
                } else if (LoopMode == 11) {
                    this.m_Paint.setColor(-1);
                } else if (LoopMode == 19) {
                    this.m_Paint.setColor(-1);
                } else {
                    this.m_Paint.setColor(-16777216);
                }
                this.m_Paint.setStyle(Paint.Style.FILL);
                this.m_Paint.setStrokeWidth(1.0f);
                this.m_Canvas.drawRect(this.dst_m, this.m_Paint);
                this.m_Canvas.drawBitmap(this.G.getBitmap(), this.src, this.dst, this.m_Paint);
                this.holder.unlockCanvasAndPost(this.m_Canvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /*private void showLanguageDialog() {
    // Cria o AlertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
    builder.setTitle("Choose language");

    // Array com as opções de idiomas
    String[] languages = {"English", "Français", "Italiano", "Deutsch", "Español", "Português", "Português Brasileiro", "日本語 (Japanese)", "简体中文 (Simplified Chinese)"};

    // Configura o que acontece quando o usuário escolhe uma opção
    builder.setItems(languages, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Chama o método initLanguage com o valor correspondente (1 a 6)
            initLanguage(which + 1); // A opção começa de 0, então somamos 1
            highscores[3] = (byte) (which + 1);
            SaveGame();
            //showResolutionDialog();
        }
    });

    builder.show();
}*/

/*private void showOptions() {
    // Cria o AlertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
    builder.setTitle("More options");

    // Array com as opções de idiomas
    String[] languages = {"Language", "Local Multiplayer", "Reset"};

    // Configura o que acontece quando o usuário escolhe uma opção
    builder.setItems(languages, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Chama o método initLanguage com o valor correspondente (1 a 6)
            if (which == 0) {
            showLanguageDialog();
            } else if (which == 1) {
            activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
            getLocalIpAddress(new Game_Draw.OnIPSelectedListener() {
        @Override
        public void onIPSelected(InetAddress selectedIP) {
            if (selectedIP != null) {
               InetAddress localAddress = selectedIP; // Define o valor de localAddress
               String ipMessage = "Your device IP: " + localAddress.getHostAddress();
               searchNetwork(ipMessage);
            } else {
            }
        }
    });
    }
        });
            } else if (which == 2) {
            LoopModeWanted = 21;
            }
        }
    });

    builder.show();
}*/

/*private void showResolutionDialog() {
    //Higher = true;
    AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
    builder.setTitle("Choose resolution");

    builder.setMessage("Would you like to use the 16:9 aspect ratio or use the full screen resolution? " +
                       "\n\nWarning: Using the full screen may cause images to appear " +
                       "distorted and/or not fit well as they are designed for 16:9 ratio.");

    builder.setPositiveButton("16:9 Aspect Ratio", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Lógica para ajustar a proporção 16:9
            HigherScale = false;
            Higher = false;
            dialog.dismiss();  // Fecha o diálogo
        }
    });

    builder.setNegativeButton("Full Screen", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Lógica para usar a resolução cheia
            HigherScale = true;
            Higher = false;
            dialog.dismiss();  // Fecha o diálogo
        }
    });

    // Exibe o AlertDialog
    builder.show();
}*/

    @Override
    public void run() {
    /*this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showResolutionDialog();
            }
        });
    while (Higher) {
        }*/
    /*this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showLanguageDialog();
            }
        });*/
        init();
        this.running = true;
        while (this.running) {
            this.lastTick = System.currentTimeMillis();
            if (Shell.sInstance.monthly_check_flag) {
                if (Shell.sInstance.monthly_mode == 1) {
                    Shell.sInstance.monthly_mode = 2;
                    Shell.sInstance.ProgressDialogStart();
                } else if (Shell.sInstance.monthly_mode == 2) {
                    try {
                        Calendar c = Calendar.getInstance();
                        String y = new StringBuilder(String.valueOf(c.get(1))).toString();
                        String m = new StringBuilder(String.valueOf(c.get(2) + 1)).toString();
                        String d = new StringBuilder(String.valueOf(c.get(5))).toString();
                        if (m.length() != 2) {
                            m = "0" + m;
                        }
                        if (d.length() != 2) {
                            d = "0" + d;
                        }
                        Shell shell = Shell.sInstance;
                        Shell.sInstance.getClass();
                        this.http_data = shell.http_connect(String.valueOf("http://puyosega-android.segamobile.jp") + "/certification?esubid=" + Shell.sInstance.getEncryptionSubID() + "&date=" + y + m + d + "&gid=" + Shell.sInstance.appid);
                        Shell.sInstance.ProgressDialogEnd();
                        Shell.sInstance.monthly_mode = 3;
                    } catch (Exception e) {
                        Shell.sInstance.ProgressDialogEnd();
                        Shell.sInstance.monthly_mode = 4;
                        Log.v("Game", "e : " + e);
                    }
                } else if (Shell.sInstance.monthly_mode == 3) {
                    try {
                        if (new String(this.http_data, "Shift_jis").split(",")[0].equals("0")) {
                            Shell.sInstance.monthly_mode = 5;
                        } else {
                            Shell.sInstance.monthly_mode = 6;
                        }
                    } catch (Exception e2) {
                        Shell.sInstance.monthly_mode = 6;
                    }
                } else if (Shell.sInstance.monthly_mode == 4) {
                    Shell.sInstance.network_error_dialog();
                    Shell.sInstance.monthly_mode = 7;
                } else if (Shell.sInstance.monthly_mode == 5) {
                    Shell.sInstance.network_ok_dialog();
                    Shell.sInstance.monthly_mode = 7;
                } else if (Shell.sInstance.monthly_mode == 6) {
                    try {
                        String temp = new String(this.http_data, "Shift_jis");
                        String[] _http_data = temp.split(",");
                        if (temp.indexOf("http://") != -1) {
                            Shell.sInstance.network_error_web_to_dialog(_http_data);
                        } else {
                            Shell.sInstance.network_error_not_web_to_dialog(_http_data);
                        }
                    } catch (Exception e3) {
                    }
                    Shell.sInstance.monthly_mode = 7;
                } else {
                    int i = Shell.sInstance.monthly_mode;
                }
            } else {
                DeviceSound.setSilent();
                if (this.musicwanted != 0) {
                    if (this.musicwanted == -1 || this.musicwanted == -2) {
                        soundtime = 0;
                        processSound(1, -1, 1);
                    }
                    if (this.musicwanted == -3) {
                        processSound(1, 2, 1);
                    }
                    if (this.musicwanted == 1) {
                        processSound(1, 1, -1);
                    }
                    if (this.musicwanted == 2) {
                        processSound(1, 2, 1);
                    }
                    if (this.musicwanted == 3) {
                        processSound(1, 3, 1);
                    }
                    if (this.musicwanted >= 100) {
                        this.mtry = this.musicwanted;
                        int f = tuneflags[this.musicwanted - GFX_BOSS_NEGATIVE_6];
                        if (f == 2) {
                            processSound(1, this.mtry, -1);
                        } else {
                            processSound(1, this.mtry, 1);
                        }
                    }
                    this.musicwanted = 0;
                }
                for (int i2 = 0; i2 < this.SFX_MAX; i2++) {
                    if (this.sfxwanted[i2] != 0) {
                        if (this.SoundAvailable != 0 && this.SoundOnOff != 0) {
                            this.DeviceSound.PlaySound(i2 + 111, 1);
                            if (bluehoops == eventbluehoops) {
                            eventbluehoops2 = 1;
                            SaveGame();
                            }
                        }
                        this.sfxwanted[i2] = 0;
                    }
                }
                if (soundtime > 0) {
                    soundtime--;
                    if (soundtime == 0 && getQed() != 0) {
                        this.mtry = getQed();
                        processSound(4, 0, 0);
                    }
                }
                if (this.vibrawanted != 0) {
                    if (this.VibraOnOff != 0) {
                        Shell.sInstance.Vibrate(this.vibrawanted << 7);
                    }
                    this.vibrawanted = 0;
                }
                if (this.key_exit != 0) {
                    this.running = false;
                    this.b_Exit = true;
                }
                doRepaint();
                this.currenttime = System.currentTimeMillis();
                this.dif = this.currenttime - this.lastTick;
                if (this.dif < MIMIMUM_FRAME_TIME) {
                    this.fps = (float) (1000 / MIMIMUM_FRAME_TIME);
                    try {
                        Thread.sleep(MIMIMUM_FRAME_TIME - this.dif);
                    } catch (Exception e4) {
                    }
                } else {
                    this.fps = (float) (1000 / this.dif);
                }
                //fps();
                
                //Test
                /*for (int i = 0; i < enemymove.length; i++) {
        enemymove[i] = 0;
    }
    if (bossmode == 0) bossmode = 3;
    ringsshown = 0;
    rings = 0;
    sonicmode = 1;
    sonicy -= 5;
    sonicacc += 5;
    inertia = 0;*/
    
            }
        }
        DeviceSound.releaseAll();
        if (this.b_Exit) {
            Shell.sInstance.finish();
            System.exit(0);
        }
    }
    
    /*public void fps() {
    long now = System.nanoTime();
    delta = (now - lastnanoTime) / 1_000_000_000.0;
    lastnanoTime = now;

    delta = Math.min(delta, 0.0625);

    //double scale = (1 / delta) / 16;
    
    //sonicacc /= scale;
}*/

    public void startDemo() {
        demoEnded = false;
        this.demoStarted = true;
    }

    protected void paint() {
        if (this.key_exit != 1) {
            if (doReset) {
                doReset = false;
                reset();
                return;
            }
            this.ringval--;
            this.ringval &= 255;
            this.h = 0;
            while (this.h < NUMBER_LOOPS) {
                if (LoopMode != this.LoopModeWanted) {
                    clearkeys();
                    if (this.LoopModeWanted == 2) {
                        setsoftkeys(0, 0);
                    }
                    LoopMode = this.LoopModeWanted;
                    this.FirstFrame = 1;
                    this.olay = 1;
                    this.MenuTimer = 10;
                    switch (LoopMode) {
                        case 1:
                        case 6:
                        case 9:
                        case 14:
                        case 24:
                            this.BlockPic = shadowstory && customFront[ssely] != null ? customFront[ssely] : this.BlockPics[ssely];
                            if (this.StripPics[ssely] == null) {
                                this.StripPics[ssely] = new DeviceImage("StripPics/" + (131073 + (this.gzone * 3)));
                            }
                            if (this.TilesPics[ssely] == null) {
                                this.TilesPics[ssely] = new DeviceImage("TilesPics/" + (131074 + (this.gzone * 3)));
                            }
                            if (this.EdgePics[ssely] == null) {
                                this.EdgePics[ssely] = new DeviceImage("EdgePics/" + (131072 + (this.gzone * 3)));
                            }
                            this.StripPic = this.StripPics[ssely];
                            this.TilesPic = this.TilesPics[ssely];
                            this.EdgePic = this.EdgePics[ssely];
                            break;
                        case 20:
                        case 22:
                            int p = this.csnum * 5;
                            if (this.CSPics[this.cslist[p]] == null) {
                                this.CSPics[this.cslist[p]] = new DeviceImage(65536 + (this.cslist[p] - 1));
                            }
                            if (this.CSPics[this.cslist[p + 1]] == null && this.cslist[p + 1] != 0) {
                                this.CSPics[this.cslist[p + 1]] = new DeviceImage(65536 + (this.cslist[p + 1] - 1));
                            }
                            if (this.CSPics[this.cslist[p + 2]] == null && this.cslist[p + 2] != 0) {
                                this.CSPics[this.cslist[p + 2]] = new DeviceImage(65536 + (this.cslist[p + 2] - 1));
                            }
                            this.CSPic1 = this.CSPics[this.cslist[p]];
                            this.CSPic2 = this.CSPics[this.cslist[p + 1]];
                            this.CSPic3 = this.CSPics[this.cslist[p + 2]];
                            break;
                    }
                }
                if (LoopMode == -2) {
                    if (this.choice == 1 || this.key_fire == 1) {
                        Standard.pressConfirm();
                    } else if (this.choice == 2) {
                        Standard.pressCancel();
                    }
                    this.key_fire = 0;
                    this.choice = 0;
                    int value = Standard.execMoreGame(true);
                    if (value == 0) {
                        this.key_exit = 1;
                    }
                } else if (LoopMode == 1) {
                    ProcessGame();
                } else if (LoopMode == 2) {
                    ProcessMainMenu();
                } else if (LoopMode == 3) {
                    ProcessLangMenu();
                } else if (LoopMode == 4) {
                    ProcessHighScores();
                } else if (LoopMode == 5) {
                    ProcessZoneSelect();
                } else if (LoopMode == 9) {
                    ProcessReady();
                } else if (LoopMode == 6) {
                    ProcessLevelFailed();
                } else if (LoopMode == 7) {
                    ProcessInterrupted();
                } else if (LoopMode == 8) {
                    ProcessExitConfirm();
                } else if (LoopMode == 34) {
                    ProcessLinkConfirm();
                } else if (LoopMode == 10) {
                    ProcessFirstSplash();
                } else if (LoopMode == 11) {
                    ProcessSecondSplash();
                } else if (LoopMode == 19) {
                    ProcessSecondSplashB();
                } else if (LoopMode == 17) {
                    ProcessSoundMenu();
                } else if (LoopMode == 12) {
                    ProcessThirdSplash();
                } else if (LoopMode == 13) {
                    ProcessPausescreen();
                } else if (LoopMode == 14) {
                    ProcessLevelDone();
                } else if (LoopMode == 15) {
                    ProcessOptions();
                } else if (LoopMode == 16) {
                    ProcessInfo();
                } else if (LoopMode == 20) {
                    ProcessFin();
                } else if (LoopMode == 21) {
                    ProcessDelRecords();
                } else if (LoopMode == 22) {
                    ProcessCutscene();
                } else if (LoopMode == 24) {
                    ProcessResults();
                } else if (LoopMode == 26) {
                    ProcessConfirmMain();
                } else if (LoopMode == 27) {
                    ProcessUpsell();
                } else if (LoopMode == 28) {
                    ProcessLaunch();
                } else if (LoopMode == 29) {
                    ProcessRotation();
                } else if (LoopMode == 31) {
                    ProcessDemoStart();
                } else if (LoopMode == 32) {
                    ProcessDemoEnd();
                } else if (LoopMode == 33) {
                    ProcessNokiaSku();
                } else if (LoopMode == 35) {
                    ProcessSelect();
                } else if (LoopMode == 36) {
                    ProcessEvent();
                } else if (LoopMode == 37) {
                    ProcessHS();
                } else if (LoopMode == 38) {
                    ProcessHS2();
                } else if (LoopMode == 39) {
                    ProcessUNUSED();
                } else if (LoopMode == 40) {
                    ProcessCutsceneFromIni();
                } else if (LoopMode == 41) {
                    ProcessStorySelect();
                } else if (LoopMode == 42) {
                    ProcessGraphicsOptions();
                }
                this.h++;
            }
            if (flashTimer < 10) {
                flashTimer--;
            }
            flushTouch();
            this.slidexoff = this.slidex;
            this.slideyoff = this.slidey;
            if (LoopMode == -2) {
                Standard.drawMoreGame(this.G, this.GameWidth, this.GameHeight);
                flash();
                return;
            }
            if (LoopMode == 1) {
                DrawGame();
            } else if (LoopMode == 2) {
                DrawMainMenu();
            } else if (LoopMode == 3) {
                DrawLangMenu();
            } else if (LoopMode == 5) {
                DrawZoneSelect();
            } else if (LoopMode == 4) {
                DrawHighScores();
            } else if (LoopMode == 9) {
                DrawReady();
            } else if (LoopMode == 6) {
                DrawLevelFailed();
            } else if (LoopMode == 7) {
                DrawInterrupted();
            } else if (LoopMode == 8) {
                DrawExitConfirm();
            } else if (LoopMode == 34) {
                DrawLinkConfirm();
            } else if (LoopMode == 10) {
                DrawFirstSplash();
            } else if (LoopMode == 11) {
                DrawSecondSplash();
            } else if (LoopMode == 12) {
                DrawThirdSplash();
            } else if (LoopMode == 13) {
                DrawPausescreen();
            } else if (LoopMode == 14) {
                DrawLevelDone();
            } else if (LoopMode == 15) {
                DrawOptions();
            } else if (LoopMode == 16) {
                DrawInfo();
            } else if (LoopMode == 17) {
                DrawSoundMenu();
            } else if (LoopMode == 19) {
                DrawSecondSplashB();
            } else if (LoopMode == 20) {
                DrawFin();
            } else if (LoopMode == 21) {
                DrawDelRecords();
            } else if (LoopMode == 22) {
                DrawCutscene();
            } else if (LoopMode == 24) {
                DrawResults();
            } else if (LoopMode == 26) {
                DrawConfirmMain();
            } else if (LoopMode == 27) {
                DrawInfo();
            } else if (LoopMode == 28) {
                DrawLaunch();
            } else if (LoopMode == 29) {
                DrawRotation();
            } else if (LoopMode == 31) {
                DrawDemoStart();
            } else if (LoopMode == 32) {
                DrawDemoEnd();
            } else if (LoopMode == 33) {
                DrawInfo();
            } else if (LoopMode == 35) {
                DrawSelect();
            } else if (LoopMode == 36) {
                DrawEvent();
            } else if (LoopMode == 37) {
                DrawHS();
            } else if (LoopMode == 38) {
                DrawHS2();
            } else if (LoopMode == 39) {
                DrawUNUSED();
            } else if (LoopMode == 40) {
                DrawCutsceneFromIni();
            } else if (LoopMode == 41) {
                DrawStorySelect();
            } else if (LoopMode == 42) {
                DrawGraphicsOptions();
            }
            slidex = (slidex * 2) / 3;
            slidey = (slidey * 2) / 3;
            slidexoff = 0;
            slideyoff = 0;
            int kr = sk2;
            if (skflip != 0) {
                int i = sk2;
                kr = sk1;
            }
            if (LoopMode != 1) {
                if (kr > 6) {
                    ClivePrint(0, (GameHeight - G.getFontSize()) - 2, localText[SKptrs[kr]], 2, Color_White, false);
                } else if (kr > 0) {
                    Blit(softkey, (GameWidth - GFX_EXPLOSION_C) - 1, (GameHeight - GFX_SONIC_HURT_FLIP) - 1, GFX_EXPLOSION_C, GFX_SONIC_HURT_FLIP, (kr - 1) * GFX_EXPLOSION_C, 0);
                }
            }
            flash();
            m_bNeedTitleBg = false;
            StartSoundPlayed = false;
        }
    }

    public static void doRepaint() {
        frame.paint();
    }
    
    /*             Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
                    try {
                        Thread.sleep(MIMIMUM_FRAME_TIME - dif);
                    } catch (Exception e4) {
                    }
            Shell.sInstance.runOnUiThread(new Runnable() {
    @Override
    public void run() {
            }
});
}
});
thread.start();*/

    private DeviceImage LoadCSPic(DeviceImage i, int pnum) {
        if (pnum == 0) {
            return null;
        }
        DeviceImage i2 = new DeviceImage(65536 + (pnum - 1));
        return i2;
    }

    private void Mad(boolean cond, String s, int newcolor, boolean shadow) {
        this.o = 0;
        if (cond) {
            if (this.slidexoff == 0) {
                drawItemFlash((this.slideyoff + this.drawY) - 11);
            }
            ClivePrint(this.o, this.drawY - 2, s, 1, newcolor, shadow);
            int f = (this.ringval & 3) + 113;

            int i = 0;
            if (!RegularFont && !CurrentL.equals("jp") && !CurrentL.equals("ch")) {
               for (int i2 = 0; i2 < s.length(); i2++) {
                    char c = s.charAt(i2);
                    if (c == 73 || c == 204  || c == 205 || c == 48 || c == 50 || c == 52) {
                        i += 4;
                    }
                }
            }
            /*if (!CurrentL.equals("jp") && !CurrentL.equals("ch")) {
            i = 180 - (i/2 -14);
            }*/
            FBlit(this.fontImage, -i + ((this.lastxprint - 14) - (this.framelist[f << 2] >> 1)) + 5, (this.slideyoff + this.drawY) - 1, f);
        } else {
            ClivePrint(this.o, this.drawY - 2, s, 1, newcolor, shadow);
        }
        this.drawY += GFX_HUD_LIVES;
    }

    private void ClivePrint(int x, int y, String t, int flags, int newcolor, boolean shadow) {
        int clipx = this.G.getClipX();
        int clipy = this.G.getClipY();
        int clipw = this.G.getClipWidth();
        int cliph = this.G.getClipHeight();
        int color = this.G.getColor();
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.setColor(newcolor);
        if (flags == 1) {
            int x2 = x + (this.GameWidth >> 1);
            this.G.setColor(0);
            this.G.drawString(t, this.slidexoff + x2 + 1, (this.slideyoff + y) - 7, 1536);
            this.G.drawString(t, this.slidexoff + x2, ((this.slideyoff + y) - 7) + 1, 1536);
            this.G.setColor(newcolor);
            this.G.drawString(t, this.slidexoff + x2, (this.slideyoff + y) - 7, 1536);
            this.lastxprint = ((this.slidexoff + x2) - (this.G.stringWidth(t) / 2)) - 5;
        } else if (flags == 2) {
            int x3 = (this.GameWidth - 1) - x;
            this.G.setColor(0);
            this.G.drawString(t, this.slidexoff + x3 + 1, (this.slideyoff + y) - 7, 5120);
            this.G.drawString(t, this.slidexoff + x3, ((this.slideyoff + y) - 7) + 1, 5120);
            this.G.setColor(newcolor);
            this.G.drawString(t, this.slidexoff + x3, (this.slideyoff + y) - 7, 5120);
            this.lastxprint = ((this.slidexoff + x3) - this.G.stringWidth(t)) - 5;
        } else {
            this.G.setColor(0);
            this.G.drawString(t, this.slidexoff + x + 1, (this.slideyoff + y) - 7, 1280);
            this.G.drawString(t, this.slidexoff + x, ((this.slideyoff + y) - 7) + 1, 1280);
            this.G.setColor(newcolor);
            this.G.drawString(t, this.slidexoff + x, (this.slideyoff + y) - 7, 1280);
            this.lastxprint = (this.slidexoff + x) - 5;
        }
        this.G.setClip(clipx, clipy, clipw, cliph);
        this.G.setColor(color);
    }

    private void tinyprint(int x, int y, String t, int flags) {
        int clipx = this.G.getClipX();
        int clipy = this.G.getClipY();
        int clipw = this.G.getClipWidth();
        int cliph = this.G.getClipHeight();
        int color = this.G.getColor();
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.setColor(255, 255, 255);
        if (flags == 4) {
            this.G.setColor(0);
            this.G.drawString(t, this.slidexoff + x + 1, this.slideyoff + y, 1536);
            this.G.drawString(t, this.slidexoff + x, this.slideyoff + y + 1, 1536);
            this.G.setColor(255, 255, 255);
            this.G.drawString(t, this.slidexoff + x, this.slideyoff + y, 1536);
        } else {
            this.G.setColor(0);
            this.G.drawString(t, this.slidexoff + x + 1, this.slideyoff + y, 1280);
            this.G.drawString(t, this.slidexoff + x, this.slideyoff + y + 1, 1280);
            this.G.setColor(255, 255, 255);
            this.G.drawString(t, this.slidexoff + x, this.slideyoff + y, 1280);
        }
        this.G.setClip(clipx, clipy, clipw, cliph);
        this.G.setColor(color);
    }

    private void hugeprint(int y, String t) {
        int clipx = this.G.getClipX();
        int clipy = this.G.getClipY();
        int clipw = this.G.getClipWidth();
        int cliph = this.G.getClipHeight();
        int color = this.G.getColor();
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.setColor(255, 255, 255);
        this.G.setColor(0);
        this.G.drawString(t, (this.GameWidth / 2) + 1, y, 1536);
        this.G.drawString(t, this.GameWidth / 2, y + 1, 1536);
        this.G.setColor(255, 255, 255);
        this.G.drawString(t, this.GameWidth / 2, y, 1536);
        this.G.setClip(clipx, clipy, clipw, cliph);
        this.G.setColor(color);
    }

    private void drawCliveNum(String num, int x, int y, int flags) {
        int n = num.length();
        char[] ca = num.toCharArray();
        if (flags == 1) {
            x -= ((17 * n) / 2) + (this.GameWidth >> 1);
        } else if (flags == 2) {
            x = (this.GameWidth - x) - (17 * n);
        }
        for (int i = 0; i < n; i++) {
            int c = ca[i] - GFX_EXPLOSION_D;
            Blit(this.fontImage, x + (17 * i), y, 17, 22, c * 17, GFX_EXPLOSION_D);
        }
    }

    private void HTad(String s) {
        hugeprint(this.drawY, s);
        this.drawY += GFX_HUD_LIVES;
    }

    private void Clr(int r, int g, int b) {
        if (G != null) {
        this.G.setColor(r, g, b);
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.fillRect(0, 0, this.GameWidth, this.GameHeight);
        }
    }

    private void Cls(int bgr) {
        this.FlagVal++;
        Clr(GFX_ZONE_3_ENEMY_A, 150, 253);
        if (bgr == 1) {
            this.m_bNeedTitleBg = true;
            if (this.MenuPic != null) {
            this.MenuPic.drawImage(this.G, this.GameWidth >> 1, this.GameHeight >> 1, 8704);
            }
            this.drawY = 10;
        }
        this.drawY = 10;
        this.drawY += 30;
    }

    private void DrawThirdSplash() {
        if (this.TitlePic != null) {
            this.m_bNeedTitleBg = true;
            this.FlagVal++;
            Clr(GFX_ZONE_3_ENEMY_A, 150, 253);
            this.TitlePic.drawImage(this.G, this.GameWidth >> 1, this.GameHeight >> 1, 8704);
            if (this.highscores[28] == 2 && this.highscores[29] == 2 && this.highscores[30] == 2 && this.DiamondPic != null) {
                this.DiamondPic.drawImage(this.G, this.GameWidth >> 1, 15, 8704);
            }
            if ((this.FlagVal & 4) == 0) {
                ClivePrint(0, (this.GameHeight * GFX_SHARD_1) / 100, localText[32], 1, Color_White, true);
                ClivePrint(0, this.G.getFontSize() + ((this.GameHeight * GFX_SMOKE_4) / 100), localText[33], 1, Color_White, false);
            }
            this.ink = 0;
            this.teletype = 0;
            this.telemax = SFX_last;
            tinyprint(2, this.GameHeight - 30, "©SEGA", 0);
        }
    }

    private void DrawSecondSplash() {
        DrawLoadingBar(GFX_ZONE_5_ENEMY_B_FLIP - (this.MenuTimer / 8));
    }

    private void DrawSecondSplashB() {
        DrawLoadingBar(110 - (this.MenuTimer / 8));
    }

    private void DrawFirstSplash() {
        DrawLoadingBar(GFX_ANTENNA_OFF - (this.MenuTimer / 8));
    }
    public void DrawFirstSplash2(Bitmap bm) {
    if (GFX_ANTENNA_OFF - (MenuTimer / 8) < 39) {
        DrawLoadingBar2(GFX_ANTENNA_OFF - (this.MenuTimer / 8), bm);
        }
    }

    private void DrawLoadingBar(int perc) {
        if (this.G != null) {
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        if (!aIntro) {
        Clr(255, 255, 255);
        //Clr(0, 0, 0);
        this.G.wg_drawImage(this.DevPic, this.GameWidth >> 1, this.GameHeight >> 1, 8704);
        }
        this.G.setColor(0, 0, 100);
        this.G.fillRect((this.GameWidth >> 1) - GFX_BLUE_BONUS_FRONT, this.GameHeight - 10, 112, 4);
        this.G.setColor(GFX_ZONE_2_ENEMY_A_FLIP, GFX_ANTENNA_OFF, GFX_BALL6);
        this.G.fillRect((this.GameWidth >> 1) - GFX_GREEN_BONUS_GO_B, this.GameHeight - 9, 110, 2);
        this.G.setColor(239, 144, 16);
        this.G.fillRect((this.GameWidth >> 1) - GFX_GREEN_BONUS_GO_B, this.GameHeight - 9, perc, 2);
        }
    }
    private void DrawLoadingBar2(int perc, Bitmap bm) {
        if (this.G != null) {
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        }
        if (bm != null) {
        this.G.wg_drawImage(bm, this.GameWidth >> 1, this.GameHeight >> 1, 8704);
        }
        //DrawLoadingBar(perc);
    }

    public void drawItemFlash(int pos) {
        if (flashTimer % 2 != 0) {
            this.G.setColor(Color_White);
        } else {
            this.G.setColor(6182932);
        }
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.fillRect(12, pos, this.GameWidth - 24, GFX_HUD_LIVES);
    }

    private void Tad(String s, int newcolor, boolean shadow) {
        ClivePrint(0, this.drawY, s, 1, newcolor, shadow);
        this.drawY += GFX_HUD_LIVES;
    }

    private void Gap() {
        this.drawY += 6;
    }

    void bluebox(int bh) {
        if (this.G != null) this.drawY = ((this.GameHeight - this.G.getFontSize()) - this.boxh) >> 1;
        this.boxh = bh;
        int bw2 = 2 << 1;
        int bw = 2 + this.slidexoff;
        int y = this.slideyoff + this.drawY;
        int w1 = (this.GameWidth - bw2) - 10;
        this.boxh -= 38;
        int y2 = (y - 6) - 3;
        this.boxh += 8;
        this.lefttodraw = 0;
        this.G.setColor(186, 177, GFX_BOSS_5);
        this.G.fillRect(bw + 10, y2 + 33, w1, this.boxh);
        if (w1 <= 306) {
            Blit(this.fontImage, bw, y2, w1, 33, 0, GFX_BOSS_NEGATIVE_1);
        } else {
            Blit(this.fontImage, bw, y2, 306, 33, 0, GFX_BOSS_NEGATIVE_1);
            Blit(this.fontImage, bw + 306, y2, w1 - 306, 33, 10, GFX_BOSS_NEGATIVE_1);
        }
        Blit(this.fontImage, bw + w1, y2, 10, 33, 307, GFX_BOSS_NEGATIVE_1);
        this.lefttodraw = this.boxh;
        if (this.boxh < 104) {
            Blit(this.fontImage, bw, y2 + 33, 10, this.boxh, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33, 10, this.boxh, 407, GFX_ZONE_4_ENEMY_A);
        } else if (this.boxh < 208) {
            Blit(this.fontImage, bw, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            this.remaining = this.boxh - 104;
            Blit(this.fontImage, bw, y2 + 33 + 104, 10, this.remaining + 1, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33 + 104, 10, this.remaining + 1, 407, GFX_ZONE_4_ENEMY_A);
        } else if (this.boxh < 312) {
            Blit(this.fontImage, bw, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
            this.remaining = this.boxh - 208;
            Blit(this.fontImage, bw, y2 + 33 + 208, 10, this.remaining + 2, 407, GFX_ZONE_4_ENEMY_A);
            Blit(this.fontImage, bw + w1, y2 + 33 + 208, 10, this.remaining + 2, 407, GFX_ZONE_4_ENEMY_A);
        } else if (this.boxh < 416) {
    Blit(this.fontImage, bw, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw, y2 + 33 + 208, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 208, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    this.remaining = this.boxh - 312;
    Blit(this.fontImage, bw, y2 + 33 + 312, 10, this.remaining + 3, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 312, 10, this.remaining + 3, 407, GFX_ZONE_4_ENEMY_A);
} else if (this.boxh < 520) {
    Blit(this.fontImage, bw, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 104, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw, y2 + 33 + 208, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 208, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw, y2 + 33 + 312, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 312, 10, 104, 407, GFX_ZONE_4_ENEMY_A);
    this.remaining = this.boxh - 416;
    Blit(this.fontImage, bw, y2 + 33 + 416, 10, this.remaining + 4, 407, GFX_ZONE_4_ENEMY_A);
    Blit(this.fontImage, bw + w1, y2 + 33 + 416, 10, this.remaining + 4, 407, GFX_ZONE_4_ENEMY_A);
}
        if (w1 <= 306) {
            Blit(this.fontImage, bw, y2 + 33 + this.boxh, w1, 10, 0, GFX_BALL5);
            Blit(this.fontImage, bw + w1, y2 + 33 + this.boxh, 10, 10, 307, GFX_BALL5);
        } else {
            Blit(this.fontImage, bw, y2 + 33 + this.boxh, 306, 10, 0, GFX_BALL5);
            Blit(this.fontImage, bw + 306, y2 + 33 + this.boxh, w1 - 306, 10, 10, GFX_BALL5);
            Blit(this.fontImage, bw + w1, y2 + 33 + this.boxh, 10, 10, 307, GFX_BALL5);
        }
    }

    private void DrawMainMenu() {
        Cls(1);
        this.drawY = 30;
        this.incUpsell = 0;
        
        if (DemoFurikan) {
        /*if (this.bonusunlocked == 1) {
        bluebox(190);
        } else {*/
        bluebox(220);
        //}
        Tad(localText[1], Color_White, true);
        Gap();
        Mad(this.menu[2] == 0, localText[220], Color_White, false);
        /*int bu = 0;
        if (this.bonusunlocked == 1) {
        bu = 1;*/
        Mad(this.menu[2] == 1, localText[222]/*"CHARACTER SELECT"*/, Color_White, false);
        //}
        Mad(this.menu[2] == 2, localText[4], Color_White, false);
        Mad(this.menu[2] == 3, localText[6], Color_White, false);
        Mad(this.menu[2] == 4, localText[7], Color_White, false);
        return;
        }
        if (multip) {
        bluebox(160);
        Tad(localText[1], Color_White, true);
        Gap();
        Mad(this.menu[2] == 0, localText[35], Color_White, false);
        Mad(this.menu[2] == 1, localText[221], Color_White, false);
        return;
        }
        
        if (this.upsell.equals("1") || this.upsell.equals("2")) {
            this.incUpsell = 1;
        }
        if (this.demoMode.equals("1")) {
            this.menuFix = 1;
        }
        if (this.demoMode.equals("2")) {
            this.incDemo = 1;
            this.menuFix = 2;
        }
        //if (this.bonusunlocked == 1) {
        bluebox(((this.cheatactive[0] + 6 + this.incUpsell + 1 + 1/*this.incDemo*/ + 1 + sp + multiplayerh + (multiplayerh == 0 ? 2 : 1)) * GFX_HUD_LIVES) + 6);
        /*} else {
        bluebox(((this.cheatactive[0] + 6 + this.incUpsell + 1 + 1 + 1) * GFX_HUD_LIVES) + 6);
        }*/
        Tad(localText[1], Color_White, true);
        Gap();
        if (this.demoMode.equals("1")) {
            Mad(this.menu[2] == 0, localText[188], Color_White, false);
        } else if (this.demoMode.equals("2")) {
            Mad(this.menu[2] == 0, localText[186], Color_White, false);
            Mad(this.menu[2] == 1, localText[188], Color_White, false);
        }
        if (this.demoMode.equals("0")) {
        //this.menuFix = 2;
        //this.incDemo = 1;
            Mad(this.menu[2] == 0, localText[220], Color_White, false);
            Mad(this.menu[2] == 1, localText[221], Color_White, false);
            //if (this.bonusunlocked == 1) {
        sp = 2;
        //if (!tails) {
        Mad(this.menu[2] == 2, localText[222]/*"CHARACTER SELECT"*/, Color_White, false);
        /*} else {
        Mad(this.menu[2] == 8, "CHARACTER SELECT", Color.GRAY, false);
        }*/
        //}
            if (multiplayerh == 0) Mad(this.menu[2] == 3, localText[223], Color.CYAN, false);
        }
        if (this.incUpsell == 1) {
            Mad(this.menu[2] == 4 + multiplayerh, /*localText[230]*/localText[224], Color.GREEN, false);
            Mad(this.menu[2] == 5 + multiplayerh, localText[225], Color.YELLOW, false);
        }
        Mad(this.menu[2] == 4 + sp + multiplayerh, localText[3], Color_White, false);
        Mad(this.menu[2] == 5 + sp + multiplayerh, localText[4], Color_White, false);
        Mad(this.menu[2] == 6 + sp + multiplayerh, localText[5], Color_White, false);
        Mad(this.menu[2] == 7 + sp + multiplayerh, localText[6], Color_White, false);
        Mad(this.menu[2] == 8 + sp + multiplayerh, localText[226], Color_White, false);
        if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
            Mad(this.menu[2] == (this.incUpsell + 5) + this.incDemo, localText[180], Color_White, false);
        } else {
            Mad(this.menu[2] == 9 + sp + multiplayerh, localText[7], Color_White, false);
        }
        if (multiplayerh == 0) Mad(this.menu[2] == 10 + sp + multiplayerh, "UNUSED", Color_White, false);
    }
    
    private void ProcessCutsceneFromIni() {
    if (currentIniCutscene >= iniCutscenes.size()) return;
    IniCutscene cs = iniCutscenes.get(currentIniCutscene);
    FontAdjust = false;
    BigFont = false;
    RegularFont = false;
    PauseAdjust = true;
    helpAdjust = true;
    FontRightAdjust = false;
    if (this.FirstFrame == 1) {
        this.MenuTimer = 10;
        this.FirstFrame = 0;
        if (currentIniCutscene >= 5) {
            this.musicwanted = 109;
        } else {
            this.musicwanted = 110;
        }
        setsoftkeys(3, 5);
        this.bored = 0;
        this.stringmax = this.GameWidth - 18;
        if (!cs.text.isEmpty()) {
            inittext(cs.text.get(currentLine));
        } else {
            inittext(" ");
        }
        this.telemax = 0;
    }
    if (this.MenuTimer == 0) {
        if (this.key_fire == 1) {
            this.key_fire = 0;
            this.MenuTimer = 5;
            if (this.toosoon == 0) {
                if (currentLine + 1 < cs.text.size()) {
                    currentLine++;
                    inittext(cs.text.get(currentLine));
                } else {
                    LoopMode = 23;
                    if (cs.action == 0) {
                        currentIniCutscene++;
                        currentLine = 0;
                        if (currentIniCutscene < iniCutscenes.size()) {
                            inittext(iniCutscenes.get(currentIniCutscene).text.get(0));
                        }
                    }
                    if (cs.action == -1) {
                        currentIniCutscene = 0;
                        currentLine = 0;
                        this.LoopModeWanted = 2; // menu
                    }
                    if (cs.action == -2) {
                        this.LoopModeWanted = 9; // ready
                    }
                    if (cs.action == -3) {
                        this.LoopModeWanted = 5; // zone select
                    }
                    /*if (cs.action == -6) {
                        currentIniCutscene = 0;
                        currentLine = 0;
                        this.LoopModeWanted = 20; // finish
                    }*/
                }
            } else {
                this.telemax = SFX_last;
            }
        }
        if (this.key_up > 0) {
            if (this.lineshown > 0) {
                this.lineshown--;
            }
            this.MenuTimer = 5;
        }
        if (this.key_down > 0) {
            if (this.lineshown + this.maxlines <= this.numstrings) {
                this.lineshown++;
            }
            this.MenuTimer = 5;
            return;
        }
        return;
    }
    this.MenuTimer--;
}

    private void loadCutscenesIni(Context context, String iniFile) {
    try {
        InputStream is = null;
        try {
        is = context.getAssets().open(iniFile);
        } catch (IOException e) {
        is = context.getAssets().open("shadow_campaign/cutscenes.ini");
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        IniCutscene cs = null;

        while ((line = br.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty()) continue;

            if (line.startsWith("[") && line.endsWith("]")) {
                if (cs != null) iniCutscenes.add(cs);
                cs = new IniCutscene();
            } else if (line.startsWith("image")) {
    if (cs != null) {
        String[] parts = line.substring(line.indexOf("=") + 1).split(",");
        IniImage img = new IniImage();
        img.path = parts[0].trim();
        for (String part : parts) {
            part = part.trim().toLowerCase();
            if (part.contains("left") || part.contains("right") || part.contains("center")) {
                img.pos = part;
            } else if (part.equals("flip")) {
                img.flip = true;
            } else if (part.matches("([0-9]+)(;[0-9]+)*")) {
                String[] nums = part.split(";");
                for (String num : nums) {
                    try {
                        img.showOn.add(Integer.parseInt(num));
                    } catch (NumberFormatException ignored) {}
                }
            }
        }
        cs.image.add(img);
    }
} else if (line.startsWith("text")) {
    if (cs != null) {
        String value = line.substring(line.indexOf("=") + 1);
        value = value.replace("\\n", "\n");
        cs.text.add(value.toUpperCase());
    }
} else if (line.startsWith("action=")) {
                if (cs != null) {
                    try {
                        cs.action = Integer.parseInt(line.substring("action=".length()).trim());
                    } catch (NumberFormatException e) {
                        cs.action = 0;
                    }
                }
            }
        }
        if (cs != null) iniCutscenes.add(cs);

        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

    private void DrawCutsceneFromIni() {
    if (currentIniCutscene >= iniCutscenes.size()) return;
    IniCutscene cs = iniCutscenes.get(currentIniCutscene);
    Cls(0);
    this.FlagVal++;
    this.cshigh = 370;
    this.drawY = this.cshigh + 11;
    this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
    this.panhigh = this.GameHeight - this.cshigh;
    this.G.setColor(16, GFX_GREEN_BONUS_FRONT, GFX_BALL3);
    this.G.fillRect(0, this.cshigh, this.GameWidth, this.panhigh);
    for (IniImage img : cs.image) {
    if (!img.showOn.isEmpty()) {
        int currentTextIndex = currentLine + 1;
        if (!img.showOn.contains(currentTextIndex)) continue;
    }
    try {
        Bitmap bmp = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open(img.path));
        int drawX;
        int drawY = this.GameHeight >> 2;
        int drawYimg = this.GameHeight >> 2;
        int flag = 8704;
        switch (img.pos) {
            case "left-top":
                drawX = 0;
                drawYimg = 0;
                flag = 1280; 
                break;
            case "right-top":
                drawX = this.GameWidth - 4;
                drawYimg = 0;
                break;
            case "left-lower-center":
                drawX = this.GameWidth / 4;
                drawYimg = this.cshigh;
                flag = 16640;
                break;
            case "right-lower-center":
                drawX = (this.GameWidth * 3) / 4;
                drawYimg = this.cshigh;
                flag = 20480;
                break;
            case "left-lower":
                drawX = 4;
                drawYimg = this.cshigh;
                flag = 16640;
                break;
            case "right-lower":
                drawX = this.GameWidth - 4;
                drawYimg = this.cshigh;
                flag = 20480;
                break;
            case "center-top":
                drawX = this.GameWidth / 2; 
                drawYimg = 0;
                break;
            case "left-center":
                drawX = this.GameWidth / 4;
                break;
            case "right-center":
                drawX = (this.GameWidth * 3) / 4;
                break;
            case "left":
                drawX = 4;
                break;
            case "right":
                drawX = this.GameWidth -4;
                break;
            case "center":
            default:
                drawX = this.GameWidth / 2; 
                drawYimg = this.GameHeight / 2;
                break;
        }
        if (img.flip) {
            Matrix m = new Matrix();
            m.preScale(-1, 1);
            Bitmap flipped = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, false);
            this.G.wg_drawImage(flipped, drawX, drawYimg, flag);
        } else {
            this.G.wg_drawImage(bmp, drawX, drawYimg, flag);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
    this.panhigh -= 36;
    this.maxlines = (this.panhigh - 22) / 10;
    this.G.setColor(255, 155, 0);
    this.G.fillRect(1, this.cshigh + 1, this.GameWidth - 2, this.panhigh - 2);
    outline(1, this.cshigh + 1, this.GameWidth - 2, this.panhigh - 2, 255, 255, 150, 209, GFX_BALL4, 0);
    outline(7, this.cshigh + 1 + 6, (this.GameWidth - 2) - 12, (this.panhigh - 2) - 12, 209, GFX_BALL4, 0, 255, 255, 150);
    this.teletype = 0;
    this.toosoon = 0;
    this.n = 0;
    int shown = 0;
    while (this.n < cs.text.size()) {
        tinyprint(9, this.drawY, this.stringlist[this.n + this.lineshown], 0);
        this.drawY += 30;
        this.n++;
    }
    if (this.telemax < 9999) {
        this.telemax++;
    }
}

    private void ProcessCutscene() {
        int p = this.csnum * 5;
        FontAdjust = false;
        BigFont = false;
        RegularFont = false;
        PauseAdjust = true;
        //if (this.ssely != 0 || this.ssely != 18) 
        helpAdjust = true;
        FontRightAdjust = false;
        if (this.FirstFrame == 1) {    
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            if (this.csnum >= GFX_SMOKE_5) {
                this.musicwanted = 109;
            } else {
                this.musicwanted = 110;
            }
            setsoftkeys(3, 5);
            this.bored = 0;
            this.stringmax = this.GameWidth - 18;
            if (this.cslist[p + 3] != -1) {
                inittext(localText[this.cslist[p + 3]]);
            } else {
                inittext(" ");
            }
            this.telemax = 0;
        }
        if (this.MenuTimer == 0) {
            if (this.key_fire == 1) {
                this.key_fire = 0;
                this.MenuTimer = 5;
                if (this.toosoon == 0) {
                    if (this.lineshown + this.maxlines <= this.numstrings) {
                        this.lineshown++;
                    } else {
                        LoopMode = 23;
                        if (this.cslist[p + 4] == 0) {
                            this.csnum++;
                        }
                        if (this.cslist[p + 4] == -1) {
                            this.LoopModeWanted = 2;
                        }
                        if (this.cslist[p + 4] == -2) {
                            this.LoopModeWanted = 9;
                        }
                        if (this.cslist[p + 4] == -3) {
                            this.LoopModeWanted = 5;
                        }
                        if (this.cslist[p + 4] == -6) {
                            this.csnum = 0;
                            this.LoopModeWanted = 20;
                        }
                    }
                } else {
                    this.telemax = SFX_last;
                }
            }
            if (this.key_up > 0) {
                if (this.lineshown > 0) {
                    this.lineshown--;
                }
                this.MenuTimer = 5;
            }
            if (this.key_down > 0) {
                if (this.lineshown + this.maxlines <= this.numstrings) {
                    this.lineshown++;
                }
                this.MenuTimer = 5;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void DrawCutscene() {
        Cls(0);
        this.FlagVal++;
        this.cshigh = 370;
        this.drawY = this.cshigh + 11;
        int p = this.csnum * 5;
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        DoCS(this.CSPic1, this.cslist[p]);
        DoCS(this.CSPic2, this.cslist[p + 1]);
        DoCS(this.CSPic3, this.cslist[p + 2]);
        this.panhigh = this.GameHeight - this.cshigh;
        this.G.setColor(16, GFX_GREEN_BONUS_FRONT, GFX_BALL3);
        this.G.fillRect(0, this.cshigh, this.GameWidth, this.panhigh);
        this.panhigh -= GFX_ALTIMETER_POINTER;
        this.maxlines = (this.panhigh - 22) / 10;
        this.G.setColor(255, 155, 0);
        this.G.fillRect(1, this.cshigh + 1, this.GameWidth - 2, this.panhigh - 2);
        outline(1, this.cshigh + 1, this.GameWidth - 2, this.panhigh - 2, 255, 255, 150, 209, GFX_BALL4, 0);
        outline(7, this.cshigh + 1 + 6, (this.GameWidth - 2) - 12, (this.panhigh - 2) - 12, 209, GFX_BALL4, 0, 255, 255, 150);
        if ((this.FlagVal & 4) == 0) {
            if (this.lineshown > 0) {
                Blit(this.fontImage, (this.GameWidth - 10) >> 1, this.cshigh + 1, 9, 5, 0, 0);
            }
            if (this.lineshown + this.maxlines <= this.numstrings) {
                Blit(this.fontImage, (this.GameWidth - 10) >> 1, (this.cshigh + this.panhigh) - 6, 9, 5, 0, 4);
            }
        }
        this.teletype = 0;
        this.toosoon = 0;
        this.n = 0;
        while (this.n < this.maxlines) {
            int i = 0;
            if (!CurrentL.equals("jp") && !CurrentL.equals("ch") && Graphics.customfonts) {
            i = -4;
            }
            tinyprint(9, this.drawY + i, this.stringlist[this.n + this.lineshown], 0);
            this.drawY += 30;
            this.n++;
        }
        if (this.telemax < 9999) {
            this.telemax++;
        }
    }

    private void DoCS(DeviceImage i, int num) {
        if (num != 0) {
            int f = this.picflags[num];
            if (f == 0) {
                i.drawImage(this.G, 0, 0, 1280);
                return;
            }
            if (f == 1) {
                i.drawImage(this.G, 4, this.cshigh, 16640);
                return;
            }
            if (f == 2) {
                i.drawImage(this.G, this.GameWidth - 4, this.cshigh, 20480);
                return;
            }
            if (f == 3) {
                i.drawImage(this.G, this.GameWidth / 2, this.cshigh / 2, 8704);
                return;
            }
            if (f == 4) {
                int b = this.GameHeight;
                i.drawImage(this.G, this.GameWidth / 2, b / 2, 8704);
            } else if (f == 5) {
                i.drawImage(this.G, this.GameWidth + 44, this.cshigh, 20480);
            } else if (f == 6) {
                i.drawImage(this.G, this.GameWidth + 20, this.cshigh, 20480);
            } else if (f == 7) {
                i.drawImage(this.G, this.GameWidth + 61, this.cshigh, 20480);
            }
        }
    }

    private void DrawFin() {
        Cls(0);
        int p = this.csnum * 5;
        DoCS(this.CSPic1, this.cslist[p]);
    }
    
    private void ProcessGraphicsOptions() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(0, 2);
            this.musicwanted = 1;
            this.slidey = this.GameHeight;
            this.menu[42] = 0;
        }
        DoMenu(42, 4);
        if (this.key_back == 1) {
            this.LoopModeWanted = 15;
            return;
        }
        if (this.res == 3) {
            if (crt && crt2 != 1) {
                crt2 = 1;
            } else if (crt2 == 1) {
                crt = false;
                crt2 = 0;
            } else {
                crt = true;
                crt2 = 0;
            }
            CRTEnabled();
        }
        if (this.res == 2) {
        Graphics.customfonts = Graphics.customfonts ? false : true;
        }
        if (/*(this.bonusunlocked == 1 || backPressCount >= 7) && */this.res == 1) {
        alt = alt ? false : true;
        if (sonic) SpritePic = new DeviceImage(196619);
        else if (shadow) SpritePic = new DeviceImage("shadow");
        DiamondPic = new DeviceImage(196616);
        ZsPic = new DeviceImage(196622);
        ZrdPic = new DeviceImage(196621);
        new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
        for (int i2 = 0; i2 < 2; i2++) {
        if (CSPics[i].getId() == 65555 + i2) {
        CSPics[i] = new DeviceImage(65555 + i2);
        }
        }
        }
        int i2 = 0;
        for (int i = 0; i < 21; i++) {
        BlockPics[i] = new DeviceImage("BlockPics/" + (196608 + i2));
        StripPics[i] = new DeviceImage("StripPics/" + (131073 + (i2 * 3)));
        TilesPics[i] = new DeviceImage("TilesPics/" + (131074 + (i2 * 3)));
        EdgePics[i] = new DeviceImage("EdgePics/" + (131072 + (i2 * 3)));
        if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14) i2++;
        }
        }
        }).start();
        while (hasNull(BlockPics) || hasNull(StripPics) || hasNull(TilesPics) || hasNull(EdgePics)) {
        }
        }
        if (this.res == 0) {
        customtouchpad = customtouchpad ? false : true;
        try {
        if (customtouchpad) {
        this.touchpad[0] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_0_s40.png"));
        this.touchpad[1] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_1_s40.png"));
        this.touchpad[2] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("ok_key_0_s40.png"));
        this.touchpad[3] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("ok_key_1_s40.png"));
        this.touchpad[4] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("pause_0_s40.png"));
        this.touchpad[5] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("pause_1_s40.png"));
        } else {
        this.touchpad[0] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[1] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[2] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[3] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[4] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[5] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }
    
    private void DrawGraphicsOptions() {
        Cls(1);
        this.drawY = 30;
        bluebox((5 * 35) + 6);
        Tad(localText[235], Color_White, true);
        Gap();
        Mad(this.menu[42] == 0, customtouchpad ? localText[237] : localText[236], Color_White, false);
        Mad(this.menu[42] == 1, alt ? localText[239] : localText[238], Color_White, false);
        Mad(this.menu[42] == 2, Graphics.customfonts ? localText[241] : localText[240], Color_White, false);
        Mad(this.menu[42] == 3, crt2 != 1 ? "CRT" : "CRT2", crt ? Color.GREEN : Color_White, false);
    }

    private void DrawOptions() {
        Cls(1);
        this.drawY = 30;
        int n = 0;
        if (this.SoundAvailable == 1 || this.SoundAvailable == 2) {
            n = 1;
        }
        if (this.SoundAvailable == 3) {
            n = 2;
        }
        if (this.multiLang.equals("1")) {
            this.incLang = 1;
        }
        //int z = this.bonusunlocked == 1 || backPressCount >= 7 ? 1 : 0;
        bluebox(((n + 2 + 1 + this.incLang + 1 + 1 + 1 + 1 /*+ 1 + z*/ /*+ 1*/) * GFX_HUD_LIVES) + 6);
        Tad(localText[4], Color_White, true);
        Gap();
        if (this.SoundAvailable != 0) {
            if (this.SoundAvailable == 1) {
                if (this.SoundOnOff == 0) {
                    Mad(this.menu[15] == 0, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                } else if (this.SoundOnOff == 1) {
                    Mad(this.menu[15] == 0, localText[GFX_BLUE_BONUS_GO_B], Color_White, false);
                }
            } else if (this.SoundAvailable == 2) {
                if (this.SoundOnOff == 0) {
                    Mad(this.menu[15] == 0, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                } else if (this.SoundOnOff == 2) {
                    Mad(this.menu[15] == 0, localText[volume + GFX_BLUE_BONUS_GO_B], Color_White, false);
                }
            } else if (this.SoundOnOff == 0) {
                Mad(this.menu[15] == 0, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                Mad(this.menu[15] == 1, localText[GFX_BLUE_BONUS_GO_A], Color_White, false);
            } else if (this.SoundOnOff == 1) {
                Mad(this.menu[15] == 0, localText[volume + 211], Color_White, false);
                Mad(this.menu[15] == 1, localText[GFX_BLUE_BONUS_GO_A], Color_White, false);
            } else {
                Mad(this.menu[15] == 0, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                Mad(this.menu[15] == 1, localText[volume + 208], Color_White, false);
            }
        }
        Mad(this.menu[15] == n, localText[233], Color_White, false);
        Mad(this.menu[15] == n + 1, localText[234]/*titles[this.highscores[3] -1]*/, Color_White, false);
        if (this.VibraOnOff == 0) {
            Mad(this.menu[15] == n + 2, localText[17], Color_White, false);
        } else {
            Mad(this.menu[15] == n + 2, localText[16], Color_White, false);
        }
        if (this.TouchpadVisibleOnOff == 0) {
            Mad(this.menu[15] == n + 3, localText[207], Color_White, false);
        } else {
            Mad(this.menu[15] == n + 3, localText[206], Color_White, false);
        }
            //Mad(this.menu[15] == ((n + 1) + 1) + this.incLang + 1, localText[220], Color.CYAN, false);
            //Mad(this.menu[15] == ((n + 1) + 1) + this.incLang + 1 + 1, "FPS: " + FPSV, Color_White, false);
            /*String[] titles = {
        "SOUND TEST",     // 1 - Inglês
        "TEST SONORE",    // 2 - Francês
        "TEST SUONO",     // 3 - Italiano
        "KLANGTEST",      // 4 - Alemão (Deutsch)
        "PRUEBA DE SONIDO", // 5 - Espanhol
        "TESTE DE SOM",   // 6 - Português
        "TESTE DE SOM",   // 7 - Português Brasileiro
        "サウンドテスト",   // 8 - Japonês
        "声音测试"         // 9 - Chinês Simplificado
    };*/
            //Mad(this.menu[15] == n + 4, customtouchpad ? localText[233] : localText[232], Color_White, false);
            Mad(this.menu[15] == n + 4, localText[235], Color_White, false);
            if (this.incLang == 1) {
            Mad(this.menu[15] == n + 4 + 1, localText[18], Color_White, false);
            }
            Mad(this.menu[15] == n + 4 + 1 + this.incLang, localText[GFX_SONIC_DEAD_FLIP], Color_White, false);
            //Mad(this.menu[15] == n + 6 + this.incLang, crt2 != 1 ? "CRT" : "CRT2", crt ? Color.GREEN : Color_White, false);
            //Mad(this.menu[15] == n + 7 + this.incLang, Graphics.customfonts ? "CUSTOM FONTS: ON" : "CUSTOM FONTS: OFF", Color_White, false);
            //if (z == 1) Mad(this.menu[15] == n + 7 + this.incLang, alt ? localText[238] : localText[237], Color_White, false);
    }

    private void DrawHighScores() {
        Cls(1);
        this.drawY = 30;
        /*if (speedtime > 0 && speedtime2 == 0) {
        bluebox(290);
        } else if (speedtime2 > 0 && speedtime > 0) {
        bluebox(325);
        } else {*/
        bluebox(275);
        //}
        Tad(localText[GFX_GREEN_BONUS_GO_B], Color_White, true);
        Gap();
        /*if (speedtime > 0) {
        int minutes = Math.abs(speedtime) / 60;
        int seconds = Math.abs(speedtime) % 60;
        String timeFormatted = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds);
        Tad(localText[218] + ": " + timeFormatted, Color_White, true);
        }
        if (speedtime2 > 0) {
        int minutes2 = Math.abs(speedtime2) / 60;
        int seconds2 = Math.abs(speedtime2) % 60;
        String timeFormatted2 = minutes2 + ":" + (seconds2 < 10 ? "0" + seconds2 : seconds2);
        Tad(localText[222] + ": " + timeFormatted2, Color_White, true);
        }*/
        if ((this.FlagVal & 4) == 0) {
            this.x += this.slidexoff;
            if (this.hsy > 0) {
                Blit(this.fontImage, ((this.GameWidth - 10) >> 1) + this.slidexoff, (this.slideyoff + this.drawY) - 6, 12, 7, 0, 0);
            }
            if (this.hsy < 6 && this.highscores[(this.hsy * 3) + 10 + this.hsx + 3] != 0) {
                Blit(this.fontImage, ((this.GameWidth - 10) >> 1) + this.slidexoff, ((((this.slideyoff + this.drawY) + 275) - GFX_HUD_LIVES) - 6) - 15, 12, 7, 0, 7);
            }
            if (this.hsx > 0) {
                Blit(this.fontImage, this.slidexoff + GFX_SONIC_SPINNING_C_FLIP, (((this.slideyoff + this.drawY) + (275 / 2)) - GFX_HUD_LIVES) - 6, 7, 12, 13, 1);
            }
            if (this.hsx < 2 && this.highscores[(this.hsy * 3) + 10 + this.hsx + 1] != 0) {
                Blit(this.fontImage, (this.slidexoff + this.GameWidth) - 30, (((this.slideyoff + this.drawY) + (275 / 2)) - GFX_HUD_LIVES) - 6, 7, 12, 20, 1);
            }
        }
        Gap();
        Tad(localText[this.hsy + GFX_ZONE_1_ENEMY_B_FLIP], Color_White, false);
        Tad(localText[34], Color_White, false);
        Tad(String.valueOf(localText[GFX_SONIC_HURT_FLIP]) + (this.hsx + 1), Color_White, false);
        Gap();
        int i = 0;
        int i2 = 0;
        if (!CurrentL.equals("jp") && !CurrentL.equals("ch") && Graphics.customfonts) {
            i = 105;
            i2 = -105;
        }
        for (int n = 0; n < 3; n++) {
            int ch = (this.hsy * 9) + (this.hsx * 3) + n;
            if (this.lasthiscore != ch || (this.FlagVal & 4) == 0) {
                int s = getscore(this.hsx + (this.hsy * 3), n);
                if (s < 0) {
                    if (s == -200) {
                    }
                    if (s == -100) {
                    }
                    s = -s;
                }
                FontAdjust = false;
                FontRightAdjust = true;
                ClivePrint(i + 130, this.drawY, "     " + s, 0, Color_White, false);
                Blit(this.fontImage, i2 + this.lastxprint - GFX_SONIC_SPINNING_C_FLIP, (this.drawY + this.slideyoff) - 4, 22, 22, (n * 22) + 168, 136);
                FontRightAdjust = false;
                FontAdjust = true;
            }
            this.drawY += GFX_HUD_LIVES;
        }  
    }

    private void DrawLangMenu() {
        if (!this.multiLang.equals("lang.dat")) {
            int n = 0;
            if (this.ilang > 0) {
                n = 1;
            }
            Cls(n);
            this.drawY = 30;
            int i = 7 * GFX_HUD_LIVES;
            bluebox(100 + 18 + 245);
            if (n == 1) {
                Tad(localText[18], Color_White, false);
            } else {
                Tad("LANGUAGE", Color_White, false);
            }
            Gap();
            
            Mad(this.menu[3] == 0, "ENGLISH", Color_White, false);
            Mad(this.menu[3] == 1, "FRANÇAIS", Color_White, false);
            Mad(this.menu[3] == 2, "ITALIANO", Color_White, false);
            Mad(this.menu[3] == 3, "DEUTSCH", Color_White, false);
            Mad(this.menu[3] == 4, "ESPAÑOL", Color_White, false);
            Mad(this.menu[3] == 5, "PORTUGUÊS", Color_White, false);
            Mad(this.menu[3] == 6, "PORTUGUÊS BRASILEIRO", Color_White, false);
            Mad(this.menu[3] == 7, "日本語", Color_White, false);
            Mad(this.menu[3] == 8, "简体中文", Color_White, false);
        }
    }

    private void DrawDelRecords() {
        Cls(1);
        this.drawY = 30;
        bluebox(263);
        Tad(String.valueOf(localText[GFX_SONIC_DEAD_FLIP]) + localText[28], Color_White, true);
        Gap();
        Tad(localText[GFX_SPRING_SQUASHED], Color_White, false);
        Tad(localText[GFX_GREEN_BONUS_FRONT], Color_White, false);
        Tad(localText[GFX_GREEN_BONUS_GO_A], Color_White, false);
        Gap();
        Gap();
        Mad(this.menu[21] == 0, localText[GFX_SONIC_SPINNING_C_FLIP], Color_White, false);
        Gap();
        Mad(this.menu[21] == 1, localText[24], Color_White, false);
    }

    private void DrawSoundMenu() {
        Cls(0);
        this.drawY = 30;
        bluebox(148);
        Tad(localText[233], Color_White, true);
        Gap();
        Mad(this.menu[17] == 0, localText[217]/*localText[GFX_SONIC_SPINNING_C_FLIP]*/, Color_White, false);
        Mad(this.menu[17] == 1, localText[218]/*localText[24]*/, Color_White, false);
        Mad(this.menu[17] == 2, localText[219]/*localText[24]*/, Color_White, false);
    }

    private void DrawInterrupted() {
        Cls(0);
        this.drawY += 200;
        boolean y = FontAdjust;
        boolean z = PauseAdjust && !y;
        if (BigFont && !RegularFont) FontAdjust = false;
        else if (!helpAdjust && !FontAdjust || z) FontAdjust = true;
        Tad(localText[26], Color_White, false);
        Gap();
        Gap();
        Gap();
        Tad(localText[27], Color_White, false);
        if (!helpAdjust && !y || z) FontAdjust = false;
    }

    private void DrawExitConfirm() {
        Cls(1);
        this.drawY = 30;
        bluebox(146);
        Tad(String.valueOf(localText[22] + localText[28]), Color_White, true);
        Gap();
        Tad(localText[29], Color_White, false);
        Mad(this.menu[8] == 0, localText[GFX_SONIC_SPINNING_C_FLIP], Color_White, false);
        Mad(this.menu[8] == 1, localText[24], Color_White, false);
    }

    private void DrawLinkConfirm() {
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(199);
        Tad("è¿žæŽ¥ç½‘ç»œ", Color_White, true);
        Gap();
        Tad(localText[29], Color_White, false);
        Gap();
        Gap();
        Mad(this.menu[34] == 0, localText[GFX_SONIC_SPINNING_C_FLIP], Color_White, false);
        Gap();
        Mad(this.menu[34] == 1, localText[24], Color_White, false);
    }

    private void DrawConfirmMain() {
        Cls(1);
        boolean z = false;
        this.drawY = 30;
        bluebox(146);
        if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
            Tad(String.valueOf(localText[189]) + localText[28], Color_White, true);
        } else {
            if (!RegularFont) {
            BigFont = true;
            RegularFont = true;
            z = true;
            }
            Tad(String.valueOf(localText[1]) + localText[28], Color_White, true);
        }
        Gap();
        Tad(localText[29], Color_White, false);
        Mad(this.menu[26] == 0, localText[GFX_SONIC_SPINNING_C_FLIP], Color_White, false);
        Mad(this.menu[26] == 1, localText[24], Color_White, false);
        if (z) {
        RegularFont = false;
        }
    }

    private void DrawPausescreen() {
        Cls(1);
        this.drawY = 30;
        int n = 0;
        if (this.SoundAvailable == 1 || this.SoundAvailable == 2) {
            n = 1;
        }
        if (this.SoundAvailable == 3) {
            n = 2;
        }
        bluebox(((n + 6 + 1) * GFX_HUD_LIVES) + 6);
        BigFont = true;
        RegularFont = true;
        Tad(localText[26], Color_White, true);
        Gap();
        Mad(this.menu[13] == 0, localText[27], Color_White, false);
        Mad(this.menu[13] == 1, localText[20], Color_White, false);
        if (this.SoundAvailable != 0) {
            if (this.SoundAvailable == 1) {
                if (this.SoundOnOff == 0) {
                    Mad(this.menu[13] == 2, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                } else if (this.SoundOnOff == 1) {
                    Mad(this.menu[13] == 2, localText[GFX_BLUE_BONUS_GO_B], Color_White, false);
                }
            } else if (this.SoundAvailable == 2) {
                if (this.SoundOnOff == 0) {
                    Mad(this.menu[13] == 2, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                } else if (this.SoundOnOff == 2) {
                    Mad(this.menu[13] == 2, localText[GFX_BLUE_BONUS_GO_B], Color_White, false);
                }
            } else if (this.SoundOnOff == 0) {
                Mad(this.menu[13] == 2, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                Mad(this.menu[13] == 3, localText[GFX_BLUE_BONUS_GO_A], Color_White, false);
            } else if (this.SoundOnOff == 1) {
                Mad(this.menu[13] == 2, localText[volume + 211], Color_White, false);
                Mad(this.menu[13] == 3, localText[GFX_BLUE_BONUS_GO_A], Color_White, false);
            } else {
                Mad(this.menu[13] == 2, localText[GFX_ZONE_1_ENEMY_A], Color_White, false);
                Mad(this.menu[13] == 3, localText[volume + 208], Color_White, false);
            }
        }
        if (this.VibraOnOff == 0) {
            Mad(this.menu[13] == n + 2, localText[17], Color_White, false);
        } else {
            Mad(this.menu[13] == n + 2, localText[16], Color_White, false);
        }
        if (this.TouchpadVisibleOnOff == 0) {
            Mad(this.menu[13] == (n + 2) + 1, localText[207], Color_White, false);
        } else {
            Mad(this.menu[13] == (n + 2) + 1, localText[206], Color_White, false);
        }
        Mad(this.menu[13] == ((n + 2) + 1) + 1, localText[5], Color_White, false);
        if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
            Mad(this.menu[13] == ((n + 3) + 1) + 1, localText[189], Color_White, false);
        } else {
            Mad(this.menu[13] == ((n + 3) + 1) + 1, localText[1], Color_White, false);
        }
        BigFont = false;
        RegularFont = false;
    }

    private void DrawInfo() {
        this.boxh = 0;
        if (LoopMode == 33) {
            Cls(0);
            this.boxh = this.GameHeight - 10;
        } else {
            Cls(1);
            this.boxh = this.GameHeight - GFX_ANTENNA_OFF;
        }
        this.drawY = 20;
        this.boxh -= 23;
        if (this.boxh > 280) {
            this.boxh = 280;
        }
        if (LoopMode != 33) {
            bluebox(this.boxh + 0);
            if (helpAdjust) FontAdjust = true;
            RegularFont = true;
            BigFont = true;
            Tad(localText[this.textheading], Color_White, true);
        }
        Gap();
        this.maxlines = (this.boxh - GFX_HUD_LIVES) / GFX_SONIC_SPINNING_C_FLIP;
        if (this.slideyoff == 0 && (this.FlagVal & 4) == 0) {
            if (this.lineshown > 0) {
                Blit(this.fontImage, (this.GameWidth - 10) >> 1, this.drawY - 6, 12, 7, 0, 0);
            }
            if (this.lineshown + this.maxlines <= this.numstrings) {
                Blit(this.fontImage, (this.GameWidth - 10) >> 1, (this.drawY + this.boxh) - 30, 12, 7, 0, 7);
            }
        }
        this.drawY += 6;
        this.teletype = 0;
        this.telemax = SFX_last;
        this.n = 0;
        while (this.n < this.maxlines) {
            if (helpAdjust) FontAdjust = false;
            RegularFont = false;
            BigFont = false;
            if (this.textheading == 6) {
                tinyprint(this.GameWidth >> 1, this.drawY, this.stringlist[this.n + this.lineshown], 4);
            } else {
                tinyprint(30, this.drawY, this.stringlist[this.n + this.lineshown], 0);
            }
            this.drawY += GFX_SONIC_SPINNING_C_FLIP;
            this.n++;
        }
    }

    private void DrawLaunch() {
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(135);
        Tad(String.valueOf(localText[204]) + localText[28], Color_White, false);
        Gap();
        Gap();
        Mad(this.menu[28] == 0, localText[230], Color_White, false);
        Gap();
        Mad(this.menu[28] == 1, localText[231], Color_White, false);
    }
    
    private void ProcessStorySelect() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.menu[41] = 0;
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            DoMenu(41, 2);
            if (this.res == 0) {
                shadowstory = false;
                initgame();
                this.LoopModeWanted = 5;
            }
            if (this.res == 1) {
                shadowstory = true;
                initgame();
                this.LoopModeWanted = 5;
            }
            if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }
    
    private void DrawStorySelect() {
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(135);
        Tad(localText[220], Color_White, false);
        Gap();
        Gap();
        Mad(this.menu[41] == 0, localText[227], Color_White, false);
        Gap();
        Mad(this.menu[41] == 1, localText[228], Color_White, false);
    }

    private void DrawRotation() {
        Clr(255, 255, 255);
        try {
            this.brandImage.drawImage(this.G, this.GameWidth >> 1, this.GameHeight >> 1, 8704);
        } catch (Exception e) {
        }
    }

    private void DrawDemoStart() {
        String[] s = {localText[194], localText[195], localText[196]};
        if (vodaDemo.equals("1")) {
            Clr(247, 0, 0);
            this.drawY = 30;
            if (this.demoSplashImage != null) {
                this.demoSplashImage.drawImage(this.G, this.GameWidth >> 1, 10, 1536);
            }
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Tad(localText[0], Color_White, false);
            Tad(localText[179], Color_White, false);
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            for (int i = 0; i < s.length; i++) {
                if (s[i].equals("60")) {
                    Tad("60", Color_White, false);
                } else {
                    Tad(s[i], Color_White, false);
                }
            }
            return;
        }
        Cls(1);
        this.drawY = 30;
        Gap();
        Gap();
        bluebox(164);
        Tad(localText[179], Color_White, false);
        Gap();
        Gap();
        Tad("60", Color_White, false);
        for (int i2 = 0; i2 < s.length; i2++) {
            if (!s[i2].equals("60")) {
                Tad(s[i2], Color_White, false);
            }
        }
    }

    private void DrawDemoEnd() {
        if (vodaDemo.equals("1")) {
            Clr(247, 0, 0);
            this.drawY = 30;
            if (this.demoSplashImage != null) {
                this.demoSplashImage.drawImage(this.G, this.GameWidth >> 1, 10, 1536);
            }
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Gap();
            Tad(localText[0], Color_White, false);
            Tad(localText[190], Color_White, false);
            Gap();
            Gap();
            Gap();
            Tad(localText[186], Color_White, false);
            if (vodaDemo.equals("1")) {
                TadWrapped(localText[199]);
                return;
            }
            Gap();
            Tad(localText[197], Color_White, false);
            Tad(localText[198], Color_White, false);
            return;
        }
        DrawInfo();
    }

    public void TadWrapped(String st) {
        String s = "";
        int lastSpace = 0;
        String s_soFar = "";
        int i = 0;
        while (i < st.length()) {
            StringBuilder sb = new StringBuilder(String.valueOf(s));
            char c = st.charAt(i);
            s = sb.append(c).toString();
            if (c == ' ' || c == '\n') {
                s_soFar = s.trim();
                lastSpace = i;
            }
            if (s.length() * 14 > this.GameWidth - 10) {
                i = lastSpace;
                Tad(s_soFar, Color_White, false);
                s = "";
                s_soFar = "";
            }
            i++;
        }
        Tad(s, Color_White, false);
    }

    void box(int x, int y, int w, int h) {
        this.G.fillRect(x + 1, y, w - 2, h);
        this.G.fillRect(x, y + 1, w, h - 2);
    }

    void outbox(int x, int y, int w, int h, int r, int g, int b, int hr, int hg, int hb) {
        this.G.setColor(r, g, b);
        this.G.fillRect(x + 1, y, w - 2, h);
        this.G.fillRect(x, y + 1, w, h - 2);
        this.G.setColor(hr, hg, hb);
        this.G.fillRect(x + 1, y, w - 2, 1);
        this.G.fillRect(x, y + 1, 1, h - 2);
    }

    void outline(int x, int y, int w, int h, int hr, int hg, int hb, int r, int g, int b) {
        this.G.setColor(hr, hg, hb);
        this.G.drawLine(x, y - 1, (x + w) - 1, y - 1);
        this.G.drawLine(x - 1, y, x - 1, (y + h) - 1);
        this.G.setColor(r, g, b);
        this.G.drawLine(x, y + h, (x + w) - 1, y + h);
        this.G.drawLine(x + w, y, x + w, (y + h) - 1);
    }

    void inbox(int x, int y, int w, int h, int r, int g, int b, int hr, int hg, int hb) {
        this.G.setColor(r, g, b);
        this.G.fillRect(x + 1, y, w - 2, h);
        this.G.fillRect(x, y + 1, w, h - 2);
        this.G.setColor(hr, hg, hb);
        this.G.fillRect(x + 1, y + h, w - 2, 1);
        this.G.fillRect(x + w, y + 1, 1, h - 2);
        this.G.fillRect((x + w) - 1, (y + h) - 1, 1, 1);
    }

    private void DrawZoneSelect() {
        this.slideyoff = 0;
        this.slidexoff = 0;
        Cls(0);
        int showb = 0;
        if (this.bonusunlocked == 1 || this.highscores[5] == 1) {
            showb = 1;
        }
        if (showb == 1) {
            this.drawY -= 7;
        }
        this.drawY += GFX_HUD_LIVES;
        this.G.translate((this.GameWidth - 360) >> 1, GFX_SHARD_3);
        this.teletype = 0;
        this.telemax = SFX_last;
        for (int n = 0; n < showb + 6; n++) {
            int pof = 0;
            if ((n & 1) == 0) {
                pof = GFX_SPRING_SQUASHED;
            }
            Blit(this.ZsPic, 0, this.drawY + 90, 360, GFX_SPRING_SQUASHED, 0, pof);
            if (n == this.lsely && (this.FlagVal & 2) == 0) {
                if (this.lselx == 0) {
                    Blit(this.ZsPic, GFX_EXPLOSION_D, this.drawY + 90, GFX_BOSS_6, GFX_SPRING_SQUASHED, 0, pof + 204);
                }
                if (this.lselx == 1) {
                    Blit(this.ZsPic, 106, this.drawY + 90, 114, GFX_SPRING_SQUASHED, GFX_BOSS_6, pof + 204);
                }
                if (this.lselx == 2) {
                    Blit(this.ZsPic, 198, this.drawY + 90, GFX_BOSS_6, GFX_SPRING_SQUASHED, 195, pof + 204);
                }
            }
            if (this.highscores[(n * 3) + 10] == 0) {
                this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
                if (pof == 0) {
                    this.G.setColor(33, GFX_SMOKE_4, 189);
                } else {
                    this.G.setColor(16, GFX_ZONE_3_ENEMY_B_FLIP, 156);
                }
                Blit(this.ZsPic, 0, this.drawY + 90, 360, GFX_SPRING_SQUASHED, 0, pof + 396);
                Blit(this.ZsPic, 105, this.drawY + 90, 115, GFX_SPRING_SQUASHED, 0, pof + 102);
                Blit(this.ZsPic, 196, this.drawY + 90, GFX_SMOKE_1, GFX_SPRING_SQUASHED, 115, pof + 102);
            } else {
                int s = 0;
                if (this.highscores[(n * 3) + 10 + 0] == 2) {
                    s = 0 + 1;
                }
                if (this.highscores[(n * 3) + 10 + 1] == 2) {
                    s += 2;
                }
                if (this.highscores[(n * 3) + 10 + 2] == 2) {
                    s += 4;
                }
                int m = n;
                if (this.highscores[(n * 3) + 10 + 1] == 0) {
                    Blit(this.ZsPic, 105, this.drawY + 90, 115, GFX_SPRING_SQUASHED, 0, pof + 102);
                }
                if (this.highscores[(n * 3) + 10 + 2] == 0) {
                    Blit(this.ZsPic, 196, this.drawY + 90, GFX_SMOKE_3, GFX_SPRING_SQUASHED, 115, pof + 102);
                }
                Blit(this.ZsPic, 4, this.drawY + 90 + 7, GFX_ALTIMETER_POINTER, GFX_ALTIMETER_POINTER, 276, (n * GFX_ALTIMETER_POINTER) + 102);
                if ((s & 1) != 0) {
                    Blit(this.ZsPic, 292, this.drawY + 90 + 4, 27, GFX_ANTENNA_BASE, 366, (m * GFX_ANTENNA_BASE) + 102);
                } else {
                    Blit(this.ZsPic, 292, this.drawY + 90 + 4, 27, GFX_ANTENNA_BASE, 360, 0);
                }
                if ((s & 2) != 0) {
                    Blit(this.ZsPic, 307, this.drawY + 90 + 4, 24, GFX_ANTENNA_BASE, 393, (m * GFX_ANTENNA_BASE) + 102);
                } else {
                    Blit(this.ZsPic, 307, this.drawY + 90 + 4, 24, GFX_ANTENNA_BASE, 387, 0);
                }
                if ((s & 4) != 0) {
                    Blit(this.ZsPic, 319, this.drawY + 90 + 4, 27, GFX_ANTENNA_BASE, 417, (m * GFX_ANTENNA_BASE) + 102);
                } else {
                    Blit(this.ZsPic, 319, this.drawY + 90 + 4, 27, GFX_ANTENNA_BASE, 411, 0);
                }
                int i = 0;
                if (!CurrentL.equals("jp") && !CurrentL.equals("ch") && Graphics.customfonts) {
                i = -5;
                }
                tinyprint((this.GameWidth >> 1) - 10 + i, this.drawY + 90 + 10, (Online && stagename[n * 3] != null && !stagename[n * 3].isEmpty() && !stagename[n * 3].equals("null") ? stagename[n * 3] : localText[n + GFX_ZONE_1_ENEMY_B_FLIP]) + " " + localText[34], 4);
            }
            this.drawY += GFX_SPRING_SQUASHED;
        }
        this.G.translate(-((this.GameWidth - 360) >> 1), -90);
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.wg_drawImage(this.m_ZoneSelectBg, this.GameWidth / 2, this.GameHeight / 2, 512 | 8192);
    }

    private void DrawResults() {
        DrawGame();
        BigFont = true;
        RegularFont = true;
        FontAdjust = true;
        this.slidexoff = this.slidex;
        this.slideyoff = this.slidey;
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.drawY = 20;
        bluebox(298);
        Tad(localText[GFX_SONIC_HURT], Color_White, true);
        Gap();
        Tad(Online && stagename[ssely] != null && !stagename[ssely].isEmpty() && !stagename[ssely].equals("null") ? stagename[ssely] : localText[this.zone + GFX_ZONE_1_ENEMY_B_FLIP], Color_White, false);
        Tad(localText[34], Color_White, false);
        Blit(this.ZrdPic, 208, (this.slideyoff + this.drawY) - 7, GFX_ZONE_1_ENEMY_A_FLIP, 32, 0, 224);
        for (int i = 0; i < 3; i++) {
            if (this.sect == i) {
                if (this.ringsshown >= GFX_SONIC_DEAD_FLIP) {
                    if (this.spark > 0) {
                        this.spark--;
                        if (this.spark == 6) {
                            this.sfxwanted[0] = 1;
                        }
                    }
                    if (this.spark > 6) {
                        if ((this.spark - 6) % 10 > 4) {
                            Blit(this.ZrdPic, this.ZrdPixel[i][4], (this.slideyoff + this.drawY) - 7, this.ZrdPixel[i][2], this.ZrdPixel[i][3], this.ZrdPixel[i][0], this.zone * this.ZrdPixel[i][3]);
                        }
                    } else if (this.spark % 2 == 0) {
                        Blit(this.ZrdPic, this.ZrdPixel[i][4], (this.slideyoff + this.drawY) - 7, this.ZrdPixel[i][2], this.ZrdPixel[i][3], this.ZrdPixel[i][0], this.zone * this.ZrdPixel[i][3]);
                    }
                } else if (this.highscores[(this.zone * 3) + 10 + i] == 2) {
                    Blit(this.ZrdPic, this.ZrdPixel[i][4], (this.slideyoff + this.drawY) - 7, this.ZrdPixel[i][2], this.ZrdPixel[i][3], this.ZrdPixel[i][0], this.zone * this.ZrdPixel[i][3]);
                }
            } else if (this.highscores[(this.zone * 3) + 10 + i] == 2) {
                Blit(this.ZrdPic, this.ZrdPixel[i][4], (this.slideyoff + this.drawY) - 7, this.ZrdPixel[i][2], this.ZrdPixel[i][3], this.ZrdPixel[i][0], this.zone * this.ZrdPixel[i][3]);
            }
        }
        Tad(String.valueOf(localText[GFX_SONIC_HURT_FLIP]) + " " + (Online && actNumber[ssely] != -1 && actNumber[ssely] != 0 ? actNumber[ssely] : this.sect + 1), Color_White, false);
        Gap();
        int scoreX = 0;
        int scoreX2 = 0;
        int ringsX = 0;
        int ringsX2 = 0;
        int timeX = 0;
        int timeX2 = 0;
        int totalX = 0;
        int totalX2 = 0;
        int ring = 1;
        if (!CurrentL.equals("jp") && !CurrentL.equals("ch") && Graphics.customfonts) {
            scoreX2 = 0;
            ringsX = 5;
            ringsX2 = 0;
            timeX2 = 0;
            totalX2 = 0;
            scoreX = 1;
            timeX = 1;
            totalX = 1;
        }
        FontAdjust = false;
        FontRightAdjust = false;
        ClivePrint(scoreX + 15, this.drawY, localText[GFX_EXPLOSION_A], 0, Color_White, false);
        FontRightAdjust = true;
        ClivePrint(scoreX2 + 15, this.drawY, new StringBuilder().append(this.score).toString(), 2, Color_White, false);
        this.drawY += GFX_HUD_LIVES;
        FontRightAdjust = false;
        FBlit(this.ExtrasPic[ssely], ring + 15, (this.slideyoff + this.drawY) - 7, 34);
        ClivePrint(ringsX + 15 + 20, this.drawY, new StringBuilder().append(this.ringsshown).toString(), 0, Color_White, false);
        FontRightAdjust = true;
        ClivePrint(ringsX2 + 15, this.drawY, this.ringsshown + "00", 2, Color_White, false);
        this.drawY += GFX_HUD_LIVES;
        FontRightAdjust = false;
        ClivePrint(timeX + 15, this.drawY, localText[GFX_EXPLOSION_B], 0, Color_White, false);
        FontRightAdjust = true;
        ClivePrint(timeX2 + 15, this.drawY, new StringBuilder().append(this.timebonus).toString(), 2, Color_White, false);
        this.drawY += GFX_HUD_LIVES;
        Gap();
        FontRightAdjust = false;
        ClivePrint(totalX + 15, this.drawY, localText[GFX_EXPLOSION_C], 0, Color_White, false);
        FontRightAdjust = true;
        ClivePrint(totalX2 + 15, this.drawY, new StringBuilder().append(this.score + (this.ringsshown * 100) + this.timebonus).toString(), 2, Color_White, false);
        BigFont = true;
        RegularFont = true;
        FontRightAdjust = false;
        FontAdjust = true;
    }

    private void DrawReady() {
        DrawGame();
        this.drawY = 140;
        this.G.wg_setFontSize(GFX_ANTENNA_OFF);
        FontAdjust = false;
        BigFont = true;
        RegularFont = false;
        HTad(Online && stagename[ssely] != null && !stagename[ssely].isEmpty() && !stagename[ssely].equals("null") ? stagename[ssely] : localText[this.zone + GFX_ZONE_1_ENEMY_B_FLIP]);
        Gap();
        HTad(localText[34]);
        Gap();
        HTad(String.valueOf(localText[GFX_SONIC_HURT_FLIP]) + " " + (Online && actNumber[ssely] != -1 && actNumber[ssely] != 0 ? actNumber[ssely] : this.sect + 1));
        this.G.wg_setFontSize(24);
        BigFont = false;
        FontAdjust = true;
    }

    private void DrawLevelFailed() {
        DrawGame();
        this.drawY = 10;
        Tad("", Color_White, false);
        Tad("", Color_White, false);
        Tad("", Color_White, false);
        if (this.livesshown == 0) {
            this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
            bluebox(111);
            Tad(localText[GFX_ANTENNA_OFF], Color_White, false);
            Gap();
            Mad(this.menu[6] == 0, localText[19], Color_White, false);
            if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
                Mad(this.menu[6] == 1, localText[189], Color_White, false);
                return;
            } else {
                Mad(this.menu[6] == 1, localText[1], Color_White, false);
                return;
            }
        }
        Tad("", Color_White, false);
        Tad("", Color_White, false);
        this.G.wg_setFontSize(GFX_ANTENNA_OFF);
        boolean z = false;
        if (RegularFont) {
        RegularFont = false;
        z = true;
        }
        BigFont = true;
        HTad(localText[GFX_SONIC_CROUCHING]);
        BigFont = false;
        if (z) RegularFont = true;
        this.G.wg_setFontSize(24);
    }

    private void DrawLevelDone() {
        DrawGame();
        showlevdone();
    }

    void showlevdone() {
        this.drawY = 10;
        BigFont = true;
        Tad("", Color_White, false);
        Tad("", Color_White, false);
        this.G.wg_setFontSize(GFX_ANTENNA_OFF);
        HTad(Online && stagename[ssely] != null && !stagename[ssely].isEmpty() && !stagename[ssely].equals("null") ? stagename[ssely] : localText[this.zone + GFX_ZONE_1_ENEMY_B_FLIP]);
        HTad(localText[34]);
        HTad(String.valueOf(localText[GFX_SONIC_HURT_FLIP]) + " " + (Online && actNumber[ssely] != -1 && actNumber[ssely] != 0 ? actNumber[ssely] : this.sect + 1));
        HTad(localText[GFX_ALTIMETER_POINTER]);
        this.G.wg_setFontSize(24);
        Tad("", Color_White, false);
        if (this.gothiscore != 0 && (this.FlagVal & 4) == 0) {
            RegularFont = true;
            Tad(localText[GFX_ZONE_1_ENEMY_A_FLIP], Color_White, false);
            RegularFont = false;
        }
    }

    void tryaddoverlay(int g, int x, int y) {
        if (this.addoverlay == -1) {
            this.addoverlay = g;
            this.ovx = x;
            this.ovy = y;
        } else {
            this.addoverlay2 = g;
            this.ovx2 = x;
            this.ovy2 = y;
        }
    }

    private void DrawGame() {
    try {
        this.slidexoff = 0;
        this.slideyoff = 0;
        ++this.FlagVal;
        this.mo = this.scrolly * this.GameHeight >> 8;
        if (this.gzone == 1) {
            this.mo = this.scrolly * this.GameHeight >> 9;
        }

        if (this.gzone == 5) {
            this.mo = this.scrolly * this.GameHeight >> 12;
        }

        this.mo /= 8;
        this.mye = 0 - this.mo % 192;
        this.my = this.mo % zonemapheight[this.gzone];
        this.zm = this.my / 96;
        this.my -= this.zm * 96;
        this.my = 0 - this.my;
        this.zm <<= 2;
        int var2;
        int var3;
        int var4;
        DeviceImage var9;
        Graphics var10;
        int var11;
        byte var13;
        Graphics var14;
        DeviceImage var15;
        if (shadowstory && customBack[ssely] != null) {
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.wg_drawImage(customBack[ssely], 0, 0, 1280);
        } else if (this.gzone == 5) {
            for(this.h = 0; this.h < 8; ++this.h) {
                this.G.setClip(0, this.my, this.GameWidth, 96);
                this.mx = 0;

                for(this.n = 0; this.n < 4; ++this.n) {
                    var9 = this.TilesPic;
                    var10 = this.G;
                    var3 = this.mx;
                    var2 = this.my;
                    var4 = this.zonemapptr[this.zm];
                    var9.drawImage(var10, var3, var2 - var4 * 96, 1280);
                    ++this.zm;
                    this.mx += 96;
                }

                var15 = this.TilesPic;
                var14 = this.G;
                var4 = this.mx;
                var3 = this.my;
                var11 = this.zonemapptr[this.zm - 2];
                var15.drawImage(var14, var4, var3 - var11 * 96, 1280);
                this.my += 96;
            }

            this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
            if (this.zone == 5) {
                var9 = this.StripPic;
                var10 = this.G;
                var3 = this.GameHeight;
                var2 = this.maxscrolly;
                var4 = this.scrolly;
                var9.drawImage(var10, 0, var3 - 320 + (var2 - var4 >> 3), 1280);
            }
        } else {
            this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
            byte var12;
            if (this.gzone == 1) {
                this.G.setColor(63, 137, 255);
                this.G.fillRect(0, 0, this.GameWidth, this.GameHeight);
                var9 = this.EdgePic;
                var10 = this.G;
                var11 = this.EdgePic.height;
                var4 = this.maxscrolly;
                var3 = this.scrolly;
                var9.drawImage(var10, 0, 640 - var11 + (var4 - var3 >> 5), 1280);

                for(this.h = 0; this.h < 8; ++this.h) {
                    this.G.setClip(0, this.my, this.GameWidth, 96);
                    this.mx = 0;

                    for(this.n = 0; this.n < 4; ++this.n) {
                        var15 = this.TilesPic;
                        var14 = this.G;
                        var4 = this.mx;
                        var3 = this.my;
                        var12 = this.zonemapptr[this.zm];
                        var15.drawImage(var14, var4, var3 - var12 * 96, 1280);
                        ++this.zm;
                        this.mx += 96;
                    }

                    this.my += 96;
                }
            } else {
                this.StripPic.drawImage(this.G, 0, 0, 1280);

                for(this.he = 0; this.he < 6; ++this.he) {
                    this.EdgePic.drawImage(this.G, 48, this.mye, 1280);
                    this.mye += 192;
                }

                for(this.h = 0; this.h < 8; ++this.h) {
                    this.G.setClip(0, this.my, this.GameWidth, 96);
                    this.mx = 96;

                    for(this.n = 0; this.n < 3; ++this.n) {
                        var9 = this.TilesPic;
                        var10 = this.G;
                        var2 = this.mx;
                        var4 = this.my;
                        var13 = this.zonemapptr[this.zm < zonemapptr.length ? this.zm : 50];
                        var9.drawImage(var10, var2, var4 - var13 * 96, 1280);
                        ++this.zm;
                        this.mx += 96;
                    }

                    var15 = this.TilesPic;
                    var14 = this.G;
                    var3 = this.mx;
                    var4 = this.my;
                    var12 = this.zonemapptr[this.zm - 2];
                    var15.drawImage(var14, var3, var4 - var12 * 96, 1280);
                    ++this.zm;
                    this.my += 96;
                }
            }
        }

        var2 = this.GameWidth - 360 >> 1;
        this.G.translate(var2, 0);
        this.GameWidth = 360;
        this.GameHeight = 520;
        this.addoverlay = -1;
        this.addoverlay2 = -1;

        int var5;
        int var6;
        int var7;
        for(this.n = 0; this.n < this.si; var3 = this.n++) {
            this.type = this.enemytype[this.n];
            if (this.type > -1) {
                this.mby = this.enemyy[this.n] - this.scrolly;
                if (this.mby < 128 && this.mby > -16) {
                    this.mbx = this.enemyx[this.n] + 0;
                    switch (this.type) {
                        case 0:
                        case 9:
                            if (this.enemydata[this.n] > 0) {
                                this.mby += 4;
                            }

                            var9 = this.BlockPic;
                            var3 = this.mbx;
                            var5 = this.enemymove[this.n];
                            var4 = this.mby;
                            this.ScaleBlit(var9, var3 + var5, var4, 0);
                            break;
                        case 1:
                        case 13:
                            this.mbx += this.enemymove[this.n];
                            if (this.enemydata[this.n] < 4) {
                                if (this.enemydata[this.n] != 0 && this.enemydata[this.n] <= 2) {
                                    this.mby += 4;
                                }

                                var9 = this.BlockPic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.ScaleBlit(var9, var3, var4, 1);
                            } else {
                                var9 = this.BlockPic;
                                var5 = this.mbx;
                                var3 = this.mby;
                                var4 = this.enemydata[this.n];
                                this.ScaleBlit(var9, var5, var3, (var4 - 4 >> 2) + 26);
                            }
                            break;
                        case 2:
                        case 12:
                            if (this.enemydata[this.n] > 0) {
                                this.mby += 4;
                            }

                            this.mbx += this.enemymove[this.n];
                            var9 = this.BlockPic;
                            var4 = this.mbx;
                            var3 = this.mby;
                            this.ScaleBlit(var9, var4, var3, 2);
                            break;
                        case 3:
                            if (this.enemydata[this.n] == 0) {
                                this.t = this.FlagVal >> 1 & 3;
                                this.t += 6;
                                this.CentreScaleBlit(this.ExtrasPic[ssely], this.mbx, this.mby, this.t);
                            } else {
                                var9 = this.ExtrasPic[ssely];
                                var5 = this.mbx;
                                var4 = this.mby;
                                var3 = this.enemydata[this.n];
                                this.ScaleBlit(var9, var5, var4, (var3 >> 2) + 28);
                            }
                            break;
                        case 4:
                        case 11:
                            this.mbx += this.enemymove[this.n];
                            if (this.enemydata[this.n] > 0) {
                                if (this.enemydata[this.n] > 4) {
                                    this.mby += 4;
                                    var9 = this.ExtrasPic[ssely];
                                    var3 = this.mbx;
                                    var4 = this.mby;
                                    this.ScaleBlit(var9, var3, var4, 51);
                                } else {
                                    this.mby -= 2;
                                    var9 = this.ExtrasPic[ssely];
                                    var4 = this.mbx;
                                    var3 = this.mby;
                                    this.ScaleBlit(var9, var4, var3, 10);
                                }
                            } else {
                                this.ScaleBlit(this.ExtrasPic[ssely], this.mbx, this.mby, 13);
                            }
                            break;
                        case 5:
                            this.t = this.enemydata[this.n] >> 2;
                            if (this.t == 0) {
                                tmpSp = 1;
                                var9 = this.SpritePic;
                                var4 = this.mbx;
                                var3 = this.mby;
                                this.CentreScaleBlit(var9, var4, var3, 11);
                                this.dy = (int)this.sonicy - this.enemyy[this.n];
                                if (this.dy < 24 && this.dy > -16) {
                                    var4 = this.mbx;
                                    var3 = this.mby;
                                    this.tryaddoverlay(52, var4, var3 + 2);
                                } else {
                                tmpSp = 2;
                                    var9 = this.SpritePic;
                                    var4 = this.mbx;
                                    var3 = this.mby;
                                    this.CentreScaleBlit(var9, var4, var3 + 2, 52);
                                }
                            }

                            if (this.t == 1) {
                            tmpSp = 3;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 53);
                            }

                            if (this.t == 2) {
                            tmpSp = 4;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 54);
                            }
                            break;
                        case 6:
                            this.t = this.enemydata[this.n] >> 2;
                            if (this.t == 0) {
                            tmpSp = 5;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 12);
                                this.dy = (int)this.sonicy - this.enemyy[this.n];
                                if (this.dy < 24 && this.dy > -16) {
                                    var4 = this.mbx;
                                    var3 = this.mby;
                                    this.tryaddoverlay(55, var4, var3 + 2);
                                } else {
                                tmpSp = 6;
                                    var9 = this.SpritePic;
                                    var3 = this.mbx;
                                    var4 = this.mby;
                                    this.CentreScaleBlit(var9, var3, var4 + 2, 55);
                                }
                            }

                            if (this.t == 1) {
                            tmpSp = 7;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 56);
                            }

                            if (this.t == 2) {
                            tmpSp = 8;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 57);
                            }
                            break;
                        case 7:
                            if (this.enemydata[this.n] > 4) {
                                this.mby += 4;
                            }

                            if (this.gzone == 0) {
                                this.t = 58;
                                if ((this.FlagVal & 2) == 0) {
                                    this.t += 2;
                                }

                                if (this.enemydir[this.n] == 0) {
                                    ++this.t;
                                }

                                var9 = this.BlockPic;
                                var6 = this.mbx;
                                var4 = this.enemymove[this.n];
                                var5 = this.mby;
                                var3 = this.t;
                                this.CentreScaleBlit(var9, var6 + var4, var5, var3);
                            }

                            if (this.gzone == 1) {
                                this.t = 62;
                                if ((this.FlagVal & 4) == 0) {
                                    this.t += 2;
                                }

                                if (this.enemydir[this.n] == 0) {
                                    ++this.t;
                                }

                                var9 = this.BlockPic;
                                var5 = this.mbx;
                                var4 = this.enemymove[this.n];
                                var6 = this.mby;
                                var3 = this.t;
                                this.CentreScaleBlit(var9, var5 + var4, var6, var3);
                            }

                            if (this.gzone == 2) {
                                this.t = 66;
                                if ((this.FlagVal & 2) == 0) {
                                    this.t += 2;
                                }

                                if (this.enemydir[this.n] == 0) {
                                    ++this.t;
                                }

                                var9 = this.BlockPic;
                                var5 = this.mbx;
                                var4 = this.enemymove[this.n];
                                var6 = this.mby;
                                var3 = this.t;
                                this.CentreScaleBlit(var9, var5 + var4, var6, var3);
                            }

                            if (this.gzone == 3) {
                                this.t = 70;
                                if (this.enemydir[this.n] == 0) {
                                    this.t = 102;
                                }

                                if ((this.FlagVal & 2) == 0) {
                                    ++this.t;
                                }

                                var9 = this.BlockPic;
                                var4 = this.mbx;
                                var3 = this.enemymove[this.n];
                                var6 = this.mby;
                                var5 = this.t;
                                this.CentreScaleBlit(var9, var4 + var3, var6, var5);
                            }

                            if (this.gzone == 4) {
                                this.t = 72;
                                if (this.enemydir[this.n] == 0) {
                                    this.t = 74;
                                }

                                if ((this.FlagVal & 4) == 0) {
                                    ++this.t;
                                }

                                var9 = this.BlockPic;
                                var4 = this.mbx;
                                var3 = this.enemymove[this.n];
                                var5 = this.mby;
                                var6 = this.t;
                                this.CentreScaleBlit(var9, var4 + var3, var5, var6);
                            }

                            if (this.gzone == 5) {
                                this.t = this.FlagVal >> 1 & 7;
                                if (this.t > 4) {
                                    this.t = 8 - this.t;
                                }

                                this.t += 104;
                                var9 = this.BlockPic;
                                var6 = this.mbx;
                                var5 = this.enemymove[this.n];
                                var4 = this.mby;
                                var3 = this.t;
                                this.CentreScaleBlit(var9, var6 + var5, var4, var3);
                            }
                            break;
                        case 8:
                            if (this.sect < 2) {
                                this.n2 = this.n + 3;
                                this.ao = 0;
                                if (this.enemydata[this.n2] > 0) {
                                    this.ao = 4;
                                }

                                this.mbx += 4;
                                this.drawspinner(this.mbx, this.mby + this.ao);
                                tmpSp = 9;
                                var9 = this.SpritePic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                var5 = this.ao;
                                this.CentreScaleBlit(var9, var3, var4 + var5, 39);
                            }
                            break;
                        case 10:
                            var9 = this.ExtrasPic[ssely];
                            var4 = this.mbx;
                            var3 = this.mby;
                            var5 = this.enemydata[this.n];
                            this.ScaleBlit(var9, var4, var3, (var5 >> 2) + 28);
                            break;
                        case 14:
                            this.drawantenna(this.n, this.mbx, this.mby, 0);
                        case 15:
                        case 16:
                        case 17:
                        default:
                            break;
                        case 18:
                            byte[] var16 = this.enemydir;
                            var3 = this.n;
                            ++var16[var3];
                            this.t = this.enemydir[this.n] >> 1 & 3;
                            this.t += 6;
                            this.ScaleBlit(this.ExtrasPic[ssely], this.mbx, this.mby, this.t);
                            break;
                        case 19:
                            var9 = this.ExtrasPic[ssely];
                            var4 = this.mbx;
                            var5 = this.mby;
                            var3 = this.enemydata[this.n];
                            this.ScaleBlit(var9, var4, var5, (var3 >> 1) + 45);
                            break;
                        case 20:
                            if (this.bossbob > 0) {
                                this.mby += 4;
                            }

                            if (this.bosshurt != 0 && (this.FlagVal & 2) != 0) {
                                var9 = this.ExtrasPic[ssely];
                                var5 = this.mbx;
                                var3 = this.mby;
                                var4 = this.gzone;
                                this.CentreScaleBlit(var9, var5, var3, var4 + 92);
                            } else if (this.gzone == 5) {
                                var9 = this.BlockPic;
                                var3 = this.mbx;
                                var4 = this.mby;
                                this.CentreScaleBlit(var9, var3, var4, 81);
                            } else {
                                var9 = this.ExtrasPic[ssely];
                                var5 = this.mbx;
                                var4 = this.mby;
                                var3 = this.gzone;
                                this.CentreScaleBlit(var9, var5, var4, var3 + 76);
                            }

                            if (this.gzone == 3) {
                                var4 = this.mbx;
                                var5 = this.mby;
                                var3 = this.mbx;
                                var7 = this.mby;
                                var6 = this.chain1y;
                                this.chain(var4 - 14, var5 + 10, var3 - 14, var6 + var7 + 10, 0);
                                var3 = this.mbx;
                                var4 = this.mby;
                                var7 = this.mbx;
                                var6 = this.mby;
                                var5 = this.chain2y;
                                this.chain(var3 + 14, var4 + 10, var7 + 14, var5 + var6 + 10, 0);
                            } else {
                                var7 = this.mbx;
                                int var8 = this.mby;
                                var6 = this.mbx;
                                var3 = this.chain1x;
                                var5 = this.mby;
                                var4 = this.chain1y;
                                this.chain(var7, var8 + 10, var3 + var6, var4 + var5 + 10, 0);
                                if (this.gzone >= 4) {
                                    var8 = this.mbx;
                                    var4 = this.mby;
                                    var6 = this.mbx;
                                    var3 = this.chain2x;
                                    var5 = this.mby;
                                    var7 = this.chain2y;
                                    this.chain(var8, var4 + 10, var3 + var6, var7 + var5 + 10, 1);
                                }
                            }
                            break;
                        case 21:
                            var9 = this.ExtrasPic[ssely];
                            var4 = this.mbx;
                            var3 = this.mby;
                            var5 = this.enemydata[this.n];
                            this.ScaleBlit(var9, var4, var3, (var5 >> 1) + 82);
                            break;
                        case 22:
                            this.t = this.enemydir[this.n] + 88;
                            this.ScaleBlit(this.ExtrasPic[ssely], this.mbx, this.mby, this.t);
                    }
                }
            }
        }

        this.sf = 14;
        this.needswish = -1;
        this.sonicoffset = 0;
        switch (this.sonicmode) {
            case 1:
                if (this.hurt == 1) {
                    this.sf = 43;
                } else if (this.inertia < -13) {
                    this.sf = 16;
                } else if (this.inertia > 3) {
                    this.sf = 18;
                } else {
                    this.sonicoffset = -2;
                    var3 = this.inertia;
                    this.sf = ((var3 + 13) % 3 << 1) + 20;
                    if (this.superjump != 0 && this.inertia < 0) {
                        var3 = this.inertia;
                        this.needswish = (var3 + 13) % 4 + 30;
                    }
                }
                break;
            case 2:
                this.sf = (this.tractortime % 3 << 1) + 20;
                break;
            case 3:
                this.sf = 37;
                if (this.firstjump == 0) {
                    this.sonicoffset = 4;
                }
                break;
            case 4:
                this.sf = 37;
                if (this.firstjump == 0) {
                    this.sonicoffset = 4;
                }
                break;
            case 5:
                this.sf = 49;
                break;
            case 6:
                var13 = this.enemyx[this.wonattatch];
                var4 = this.enemymove[this.wonattatch];
                float var1 = (float)(var13 + var4 + this.wonattatchx);
                this.sonicx = var1;
        }

        if (this.sonicdir > 0) {
            ++this.sf;
        }

        if (this.invulnerable == 0 || (this.FlagVal & 1) == 0) {
            tmpSp = 10;
            var9 = this.SpritePic;
            var7 = (int)this.sonicx;
            var4 = (int)this.sonicy; 
            var3 = this.scrolly;
            var6 = this.sonicoffset;
            var5 = this.sf;
            this.BaseCentreScaleBlit(var9, var7, var4 - var3 + var6 + 1, var5);
            if (this.needswish >= 0) {
                if (this.sonicdir > 0) {
                    tmpSp = 11;
                    var9 = this.SpritePic;
                    var6 = (int)this.sonicx;
                    var3 = (int)this.sonicy;
                    var4 = this.scrolly;
                    var5 = this.needswish;
                    this.BaseCentreScaleBlit(var9, var6, var3 - var4, var5 + 98 - 30);
                } else {
                    tmpSp = 12;
                    var9 = this.SpritePic;
                    var5 = (int)this.sonicx;
                    var6 = (int)this.sonicy;
                    var3 = this.scrolly;
                    var4 = this.needswish;
                    this.BaseCentreScaleBlit(var9, var5, var6 - var3, var4);
                }
            }
        }

        if (this.addoverlay >= 0) {
            this.CentreScaleBlit(this.SpritePic, this.ovx, this.ovy, this.addoverlay);
        }

        if (this.addoverlay2 >= 0) {
            this.CentreScaleBlit(this.SpritePic, this.ovx2, this.ovy2, this.addoverlay2);
        }

        this.GameHeight = 640;
        this.GameWidth = 360;
        this.G.translate(-var2, 0);
        if (this.bonustime > 0) {
            --this.bonustime;
            if ((this.FlagVal & 2) == 0) {
                this.drawY = 70;
                boolean z = false;
                if (RegularFont) {
                RegularFont = false;
                z = true;
                }
                //if (this.bonustext == localText[41] || this.bonustext == localText[42]) {
                BigFont = true;
                //}
                this.HTad(this.bonustext);
                BigFont = false;
                if (z) RegularFont = true;
            }
        }

        if (this.bossmode != -1) {
            this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
            this.G.setColor(0, 0, 100);
            this.G.fillRect(this.GameWidth - 112 >> 1, this.GameHeight - 7, 112, 6);
            this.G.setColor(255, 0, 0);
            var14 = this.G;
            var3 = this.GameWidth;
            var2 = this.GameHeight;
            var4 = this.bosshealth;
            var14.fillRect(var3 - 110 >> 1, var2 - 6, var4 * 110 >> 8, 4);
        }

        this.FBlit(this.ExtrasPic[ssely], 2, 2, 34);
        this.drawCliveNum("" + this.rings, 34, 3, 0);
        if (!this.demoStarted) {
        if (this.cheatactive[5] == 0 || (this.FlagVal & 2) == 0) {
            this.FBlit(this.SpritePic, 3, 33, 35);
        }
        }

        if (!this.demoStarted) {
        this.drawCliveNum("" + this.lives, 47, 38, 0);
        }
        if (this.demoStarted) {
        var2 = this.secs + this.mins * 60;
        if (var2 != demoLastSecs) {
            demoLastSecs = var2;
            //++demoSecs;
            --demoSecs;
        }
        if (demoSecs == -1) {
        demoSecs = 0;
        if (!DemoFurikan) {
        this.LoopModeWanted = 6;
        } else {
        this.LoopModeWanted = 2;
        }
        }
        }
        this.drawCliveNum(this.mins % 10 + ":" + this.secs / 10 + this.secs % 10, 2, 2, 2);
        this.drawCliveNum("" + this.score, 2, 37, 2);
        if (this.demoStarted && !multip) {
        //this.drawCliveNum("" + demoSecs, 0, 38, 0);
        int minutes = Math.abs(demoSecs) / 60;
        int seconds = Math.abs(demoSecs) % 60;
        String timeFormatted = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds);
        this.drawCliveNum(timeFormatted, 0, 38, 0);
        } else if (this.demoStarted && multip) {
        //this.drawCliveNum("" + demoSecs, 2, 72, 2);
        int minutes = Math.abs(demoSecs) / 60;
        int seconds = Math.abs(demoSecs) % 60;
        String timeFormatted = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds);
        this.drawCliveNum(timeFormatted, 2, 72, 2);
        }
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.amx = this.GameWidth - 6;
        this.amy = 90;
        this.amh = 240;
        this.amf = this.amh * this.scrolly / this.maxscrolly;
        if (this.sonicmode == 6) {
            this.amf = 0;
        }

        this.G.setColor(0, 0, 0);
        this.G.fillRect(this.amx - 1, this.amy - 1, 4, this.amh + 2);
        this.G.setColor(255, 255, 0);
        this.G.fillRect(this.amx, this.amy, 2, this.amh);
        this.G.setColor(255, 0, 0);
        this.G.fillRect(this.amx, this.amy, 2, this.amf);
        this.FBlit(this.ExtrasPic[ssely], this.amx - 9, this.amy + this.amf - 5, 36);
        if (this.tut != 0 && LoopMode == 1) {
            this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
            if (this.tut2done == 0) {
                this.maxlines = 4;
            }

            if (this.tut2done == 1) {
                this.maxlines = 8;
            }

            this.drawY = 40;
            this.boxh = 0;
            if (this.tut2done == 0) {
                this.boxh = this.G.getFontSize() * 5 + 48;
            }

            if (this.tut2done == 1) {
                this.boxh = this.G.getFontSize() * 5 + 48;
            }

            this.bluebox(this.boxh);
            if (this.lineshown > 0) {
                var9 = this.fontImage;
                var2 = this.GameWidth;
                var3 = this.drawY;
                this.Blit(var9, var2 - 10 >> 1, var3 + 24, 9, 5, 0, 0);
            }

            if (this.lineshown + this.maxlines <= this.numstrings) {
                var9 = this.fontImage;
                var4 = this.GameWidth;
                var2 = this.drawY;
                var3 = this.boxh;
                this.Blit(var9, var4 - 10 >> 1, var2 + var3 + 19, 9, 5, 0, 4);
            }
            
            BigFont = true;
            RegularFont = true;
            this.Tad(localText[5], 16777215, true);
            this.Gap();
            this.teletype = 0;
            this.telemax = 9999;
            
            BigFont = false;
            RegularFont = false;
            for(this.n = 0; this.n < this.maxlines; ++this.n) {
                this.tinyprint(this.GameWidth >> 1, this.drawY, this.stringlist[this.n + this.lineshown], 4);
                this.drawY += 25;
            }
        }

        if (this.sonicmode == 6 && this.sect == 2) {
            this.showlevdone();
        }

        this.drawTouchPad(touchState);
        } catch (Exception e) {
        }
    }

    public void drawTouchPad(int _touchID) {
        Bitmap[] tmp = {this.touchpad[0], this.touchpad[0], this.touchpad[2], this.touchpad[4], this.touchpad[6], this.touchpad[6]};
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        if ((((byte) _touchID) & 4) != 0) {
            tmp[2] = this.touchpad[3];
        }
        if ((((byte) _touchID) & 8) != 0) {
            tmp[3] = this.touchpad[5];
        }
        this.G.wg_drawImage(tmp[2], this.GameWidth >> 1, this.GameHeight - 16, 16896);
        this.G.wg_drawImage(tmp[3], this.GameWidth >> 1, 0.0f, 1536);
        if (this.TouchpadVisibleOnOff != 0) {
            if ((((byte) _touchID) & 1) != 0) {
            //if (!newTouch) {
                tmp[0] = this.touchpad[1];
                /*} else {
                tmp[4] = this.touchpad[7];
                }*/
            }
            if ((((byte) _touchID) & 2) != 0) {
            //if (!newTouch) {
                tmp[1] = this.touchpad[1];
                /*} else {
                tmp[5] = this.touchpad[7];
                }*/
            }
            this.G.wg_drawImage(tmp[0], (this.GameWidth >> 1) - GFX_ZONE_2_ENEMY_A, this.GameHeight - 30, 20480);
            this.G.drawRegion(tmp[1], 0, 0, tmp[1].getWidth(), tmp[1].getHeight(), 2, (this.GameWidth >> 1) + GFX_ZONE_2_ENEMY_A, this.GameHeight - 30, 16640);
            //this.G.wg_drawImage(tmp[4], (this.GameWidth >> 1) - GFX_ZONE_2_ENEMY_A, this.GameHeight - 130, 20480);
            //this.G.drawRegion(tmp[5], 0, 0, tmp[5].getWidth(), tmp[5].getHeight(), 2, (this.GameWidth >> 1) + GFX_ZONE_2_ENEMY_A, this.GameHeight - 130, 16640);
        }
    }

    void chain(int x1, int y1, int x2, int y2, int col) {
        int y12 = y1 + this.chainfall;
        int y22 = y2 + (this.chainfall << 1);
        int x12 = (x1 * this.GameWidth) >> 7;
        int y13 = (y12 * this.GameHeight) >> 7;
        int x22 = (x2 * this.GameWidth) >> 7;
        int y23 = (y22 * this.GameHeight) >> 7;
        int dx = x22 - x12;
        int dy = y23 - y13;
        int dx2 = (dx << 8) / 7;
        int dy2 = (dy << 8) / 7;
        for (int c = 0; c < 7; c++) {
            dolink(((dx2 * c) >> 8) + x12, ((dy2 * c) >> 8) + y13);
        }
        if (this.zone == 0) {
            FBlit(this.ExtrasPic[ssely], x22 - GFX_ALTIMETER_POINTER, y23 - 12, GFX_CRUSHER);
        }
        if (this.zone == 1) {
            FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL);
        }
        if (this.zone == 2) {
            FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL2);
        }
        if (this.zone == 3) {
            FBlit(this.ExtrasPic[ssely], x22 - GFX_ALTIMETER_POINTER, y23 - 12, GFX_CRUSHER);
        }
        if (this.zone == 4) {
            FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL6);
        }
        if (this.zone == 5) {
            if (col == 0) {
                FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL3);
            } else {
                FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL4);
            }
        }
        if (this.zone == 6) {
            if (col == 0) {
                FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL5);
            } else {
                FBlit(this.ExtrasPic[ssely], x22 - 12, y23 - 12, GFX_BALL4);
            }
        }
    }

    void dolink(int x, int y) {
        FBlit(this.ExtrasPic[ssely], x - 5, y - 5, 118);
    }

    void drawantenna(int n, int mbx, int mby, int ooo) {
        int n2 = n + 1;
        int ao = 0;
        if (this.enemytype[n2] != 0) {
            n2++;
        }
        if (this.enemydata[n2] > 0) {
            ao = 4;
        }
        BaseCentreScaleBlit(this.SpritePic, mbx + 4, mby + 8 + ao, GFX_ANTENNA_BASE);
        int a = this.enemymove[n] & 15;
        int mbx2 = this.enemyx[n];
        int mby2 = ((this.enemyy[n] + ooo) + ao) - this.scrolly;
        int mbx3 = ((mbx2 * this.GameWidth) >> 7) + 7;
        int mby3 = ((mby2 * this.GameHeight) >> 7) - 6;
        int dx = (this.sintab[a] * 17) >> 8;
        int dy = (this.sintab[a + 4] * 17) >> 8;
        this.G.setClip(0, 0, this.GameWidth, this.GameHeight);
        this.G.setColor(132, 132, 132);
        this.G.drawLine(mbx3 + 6, mby3 - 2, mbx3 + dx + 6, (mby3 - dy) - 2);
        this.G.drawLine(mbx3 + 5, mby3 - 2, mbx3 + dx + 5, (mby3 - dy) - 2);
        this.G.setColor(214, 214, 214);
        this.G.drawLine(mbx3 + 3, mby3 - 2, mbx3 + dx + 3, (mby3 - dy) - 2);
        this.G.drawLine(mbx3 + 4, mby3 - 2, mbx3 + dx + 4, (mby3 - dy) - 2);
        int o = GFX_ANTENNA_OFF;
        if (this.enemymove[n] > 15 && (this.FlagVal & 4) == 0) {
            o = GFX_ANTENNA_ON;
        }
        FBlit(this.SpritePic, (mbx3 + dx) - 3, (mby3 - dy) - 5, o);
    }

    void drawspinner(int xp, int y) {
        int w = this.srot & 255;
        if (w > GFX_BALL6) {
            w = 255 - w;
        }
        int tx = 0;
        int gy = 114;
        if (this.spic == 0) {
            gy = 114 + GFX_SONIC_HURT;
        }
        int xp2 = (xp * this.GameWidth) >> 7;
        int y2 = ((y * this.GameHeight) >> 7) - 43;
        int xw = (w * GFX_ZONE_5_ENEMY_B_FLIP) >> 7;
        if (xw == GFX_ZONE_5_ENEMY_B_FLIP) {
            Blit(this.SpritePic, xp2 - GFX_SONIC_CROUCHING, y2, GFX_ZONE_5_ENEMY_B_FLIP, GFX_SONIC_HURT, 135, gy);
            return;
        }
        if (xw == 0) {
            xw = 1;
        }
        int x = xp2 - (xw >> 1);
        int step = 9525 / xw;
        for (int n = 0; n < xw; n++) {
            Blit(this.SpritePic, x, y2, 1, GFX_SONIC_HURT, (tx >> 7) + 135, gy);
            tx += step;
            x++;
        }
    }

    void ScaleBlit(DeviceImage i, int x, int y, int f) {
        int f2 = f << 2;
        Blit(i, (x * this.GameWidth) >> 7, (y * this.GameHeight) >> 7, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
    }

    void FBlit(DeviceImage i, int x, int y, int f) {
        int f2 = f << 2;
        Blit(i, x, y, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
    }

    void BaseCentreScaleBlit(DeviceImage i, int x, int y, int f) {
        int f2 = f << 2;
        /*final int f22 = f2;
        final int x2 = x;
        final int y2 = y;
        String playername = "sonic";
        if (tails) {
        playername = "tails";
        }
        if (playername != "sonic") {
        if (i == this.SpritePic) {
    int posX = ((x * GameWidth) >> 7) - (framelist[f2] >> 1);
    int posY = (((y * GameHeight) >> 7) + 2) - framelist[f2 + 1];
    if (posX >= 0 && posX <= 43 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_fallingleft");
    } else if (posX >= 44 && posX <= 98 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_effect1");
    } else if (posX >= 99 && posX <= 137 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_left");
    } else if (posX >= 138 && posX <= 176 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_ball1");
    } else if (posX >= 177 && posX <= 215 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_ball2");
    } else if (posX >= 216 && posX <= 255 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_fallingright");
    } else if (posX >= 256 && posX <= 305 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_dieleft");
    } else if (posX >= 306 && posX <= 350 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_effect2");
    } else if (posX == 0 && (posY >= 50 && posY <= 70)) {
        this.SpritePic = new DeviceImage(playername + "_flyingleft");
    } else if (posX >= 30 && posX <= 50 && posY >= 50 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_effect3");
    } else if (posX >= 150 && posX <= 170 && posY >= 190 && posY <= 200) {
        this.SpritePic = new DeviceImage(playername + "_dieright");
    } else if (posX >= 300 && posX <= 320 && posY >= 40 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_effect4");
    } else if (posX >= 30 && posX <= 50 && posY >= 100 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_effect5");
    } else if (posX >= 80 && posX <= 90 && posY >= 100 && posY <= 120) {
        this.SpritePic = new DeviceImage(playername + "_effect6");
    } else if (posX >= 300 && posX <= 320 && posY >= 90 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_effect7");
    } else if (posX >= 250 && posX <= 270 && posY >= 200 && posY <= 220) {
        this.SpritePic = new DeviceImage(playername + "_effect8");
    } else if (posX >= 80 && posX <= 100 && posY >= 50 && posY <= 70) {
        this.SpritePic = new DeviceImage(playername + "_damageleft");
    } else if (posX >= 130 && posX <= 150 && posY >= 40 && posY <= 50) {
        this.SpritePic = new DeviceImage(playername + "_ball3");
    } else if (posX >= 170 && posX <= 180 && posY >= 30 && posY <= 40) {
        this.SpritePic = new DeviceImage(playername + "_startleft");
    } else if (posX >= 250 && posX <= 270 && posY >= 60 && posY <= 70) {
        this.SpritePic = new DeviceImage(playername + "_ball4");
    } else if (posX >= 250 && posX <= 270 && posY >= 100 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_ball5");
    } else if (posX >= 250 && posX <= 270 && posY >= 130 && posY <= 140) {
        this.SpritePic = new DeviceImage(playername + "_ball6");
    } else if (posX >= 210 && posX <= 230 && posY >= 50 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_flyingright");
    } else if (posX >= 210 && posX <= 230 && posY >= 110 && posY <= 130) {
        this.SpritePic = new DeviceImage(playername + "_right");
    } else if (posX >= 210 && posX <= 230 && posY >= 260 && posY <= 280) {
        this.SpritePic = new DeviceImage(playername + "_falling");
    } else if (posX >= 217 && posX <= 227 && posY >= 210 && posY <= 220) {
        this.SpritePic = new DeviceImage(playername + "_startright");
    } else {

        Blit(i, ((x * this.GameWidth) >> 7) - (this.framelist[f2] >> 1), (((y * this.GameHeight) >> 7) + 2) - this.framelist[f2 + 1], this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
        return;
    }
    Shell.sInstance.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(Shell.sInstance, "test", Toast.LENGTH_LONG).show();
    }
    });
    //framelist[f2 + 2] = (this.SpritePic.getBitmap().getWidth() * framelist[f2 + 2]) / (((x2 * GameWidth) >> 7) - (framelist[f22] >> 1));
    //framelist[f2 + 3] = (this.SpritePic.getBitmap().getHeight() * framelist[f2 + 3]) / ((((y2 * GameHeight) >> 7) + 2) - framelist[f22 + 1]);
    Blit(this.SpritePic, ((0 * this.GameWidth) >> 7) - (this.framelist[f2] >> 1), (((0 * this.GameHeight) >> 7) + 2) - this.framelist[f2 + 1], this.SpritePic.getBitmap().getWidth(), this.SpritePic.getBitmap().getHeight(), this.framelist[f2 + 2], this.framelist[f2 + 3]);
    return;
    } else {
    Blit(i, ((x * this.GameWidth) >> 7) - (this.framelist[f2] >> 1), (((y * this.GameHeight) >> 7) + 2) - this.framelist[f2 + 1], this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
    return;
    }
        } else {*/
        Blit(i, ((x * this.GameWidth) >> 7) - (this.framelist[f2] >> 1), (((y * this.GameHeight) >> 7) + 2) - this.framelist[f2 + 1], this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
        /*return;
        }*/
    }
    /*void ttttBaseCentreScaleBlit(DeviceImage i, final int x, final int y, final int f) {
        final int f2 = f << 2;
        Shell.sInstance.runOnUiThread(new Runnable() {
    @Override
    public void run() {
                    Toast.makeText(Shell.sInstance, 
                    "tmpSp = " + tmpSp + "\n" +
    "x = " + (((x * GameWidth) >> 7) - (framelist[f2] >> 1)) + "\n" +
    "y = " + ((((y * GameHeight) >> 7) + 2) - framelist[f2 + 1]) + "\n" +
    "w = " + framelist[f2] + "\n" +
    "h = " + framelist[f2 + 1],
    Toast.LENGTH_LONG).show();
    }
    });
    }*/

    void CentreScaleBlit(DeviceImage i, int x, int y, int f) {
        int x2 = x * this.GameWidth;
        int f2 = f << 2;
        /*String playername = "sonic";
        if (tails) {
        playername = "tails";
        }
        if (playername != "sonic") {
        if (i == this.SpritePic) {
    int posX = (x2 >> 7) - (this.framelist[f2] >> 1);
    int posY = (y * this.GameHeight) >> 7;
    if (posX >= 0 && posX <= 43 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_fallingleft");
    } else if (posX >= 44 && posX <= 98 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_effect1");
    } else if (posX >= 99 && posX <= 137 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_left");
    } else if (posX >= 138 && posX <= 176 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_ball1");
    } else if (posX >= 177 && posX <= 215 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_ball2");
    } else if (posX >= 216 && posX <= 255 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_fallingright");
    } else if (posX >= 256 && posX <= 305 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_dieleft");
    } else if (posX >= 306 && posX <= 350 && posY == 0) {
        this.SpritePic = new DeviceImage(playername + "_effect2");
    } else if (posX == 0 && (posY >= 50 && posY <= 70)) {
        this.SpritePic = new DeviceImage(playername + "_flyingleft");
    } else if (posX >= 30 && posX <= 50 && posY >= 50 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_effect3");
    } else if (posX >= 150 && posX <= 170 && posY >= 190 && posY <= 200) {
        this.SpritePic = new DeviceImage(playername + "_dieright");
    } else if (posX >= 300 && posX <= 320 && posY >= 40 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_effect4");
    } else if (posX >= 30 && posX <= 50 && posY >= 100 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_effect5");
    } else if (posX >= 80 && posX <= 90 && posY >= 100 && posY <= 120) {
        this.SpritePic = new DeviceImage(playername + "_effect6");
    } else if (posX >= 300 && posX <= 320 && posY >= 90 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_effect7");
    } else if (posX >= 250 && posX <= 270 && posY >= 200 && posY <= 220) {
        this.SpritePic = new DeviceImage(playername + "_effect8");
    } else if (posX >= 80 && posX <= 100 && posY >= 50 && posY <= 70) {
        this.SpritePic = new DeviceImage(playername + "_damageleft");
    } else if (posX >= 130 && posX <= 150 && posY >= 40 && posY <= 50) {
        this.SpritePic = new DeviceImage(playername + "_ball3");
    } else if (posX >= 170 && posX <= 180 && posY >= 30 && posY <= 40) {
        this.SpritePic = new DeviceImage(playername + "_startleft");
    } else if (posX >= 250 && posX <= 270 && posY >= 60 && posY <= 70) {
        this.SpritePic = new DeviceImage(playername + "_ball4");
    } else if (posX >= 250 && posX <= 270 && posY >= 100 && posY <= 110) {
        this.SpritePic = new DeviceImage(playername + "_ball5");
    } else if (posX >= 250 && posX <= 270 && posY >= 130 && posY <= 140) {
        this.SpritePic = new DeviceImage(playername + "_ball6");
    } else if (posX >= 210 && posX <= 230 && posY >= 50 && posY <= 60) {
        this.SpritePic = new DeviceImage(playername + "_flyingright");
    } else if (posX >= 210 && posX <= 230 && posY >= 110 && posY <= 130) {
        this.SpritePic = new DeviceImage(playername + "_right");
    } else if (posX >= 210 && posX <= 230 && posY >= 260 && posY <= 280) {
        this.SpritePic = new DeviceImage(playername + "_falling");
    } else if (posX >= 217 && posX <= 227 && posY >= 210 && posY <= 220) {
        this.SpritePic = new DeviceImage(playername + "_startright");
    } else {
        Blit(i, (x2 >> 7) - (this.framelist[f2] >> 1), (y * this.GameHeight) >> 7, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
        return;
    }
    Shell.sInstance.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(Shell.sInstance, "test", Toast.LENGTH_LONG).show();
    }
    });
    Blit(this.SpritePic, (0 >> 7) - (this.framelist[f2] >> 1), (0 * this.GameHeight) >> 7, this.SpritePic.getBitmap().getWidth(), this.SpritePic.getBitmap().getHeight(), this.framelist[f2 + 2], this.framelist[f2 + 3]);
    return;
    } else {
    Blit(i, (x2 >> 7) - (this.framelist[f2] >> 1), (y * this.GameHeight) >> 7, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
    return;
    }
        } else {
        Blit(i, (x2 >> 7) - (this.framelist[f2] >> 1), (y * this.GameHeight) >> 7, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
        return;
        }*/
        Blit(i, (x2 >> 7) - (this.framelist[f2] >> 1), (y * this.GameHeight) >> 7, this.framelist[f2], this.framelist[f2 + 1], this.framelist[f2 + 2], this.framelist[f2 + 3]);
    }
    
    private void loadCustomBackIni() {
        try {
            AssetManager am = Shell.sInstance.getAssets();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(am.open("shadow_campaign/customback.ini"))
            );
    
            String line;
            int currentIndex = -1;
            String backPath = null;
            String frontPath = null;
    
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty() || line.startsWith(";") || line.startsWith("#"))
                    continue;
                if (line.startsWith("[") && line.endsWith("]")) {
                    if (currentIndex >= 0 && currentIndex < 21) {
                        if (backPath != null) {
                            customBack[currentIndex] = loadBitmapFromAssets(am, backPath);
                        }
                        if (frontPath != null) {
                            customFront[currentIndex] = new DeviceImage(frontPath);
                        }
                    }
                    currentIndex = Integer.parseInt(line.substring(1, line.length() - 1));
                    backPath = null;
                    frontPath = null;
                } else if (line.startsWith("back=")) {
                    backPath = line.substring(5).trim();
                } else if (line.startsWith("front=")) {
                    frontPath = line.substring(6).trim();
                }
            }
            if (currentIndex >= 0 && currentIndex < 21) {
                if (backPath != null) {
                    customBack[currentIndex] = loadBitmapFromAssets(am, backPath);
                }
                if (frontPath != null) {
                    customFront[currentIndex] = new DeviceImage(frontPath);
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private Bitmap loadBitmapFromAssets(AssetManager am, String path) {
        try {
            InputStream is = am.open(path);
            return BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    void Blit(DeviceImage i, int x, int y, int w, int h, int gx, int gy) {
        if (i == this.SpritePic && (this.SpritePic.getName().contains("player") || this.SpritePic.getName().equals("tails") && character.equals("tails"))) {
        w *= 2;
        h *= 2;
        gx *= 2;
        gy *= 2;
        x -= (w / 4);
        y -= (h / 4);
        }
        this.G.getClipX();
        this.G.getClipY();
        this.G.getClipWidth();
        this.G.getClipHeight();
        this.G.setClip(x, y, w, h);
        i.drawImage(this.G, x - gx, y - gy, 1280);
    }

    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (flashTimer < 10 && flashTimer >= 0) {
            return false;
        }
        
        if (keycode == KeyEvent.KEYCODE_VOLUME_UP || keycode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        return false;
    }
        
        if (keycode == KeyEvent.KEYCODE_BACK) {
        if (browser) {
        browser = false;
        Shell.sInstance.setContentView(frameLayout != null ? frameLayout : this/*Shell.sInstance.getCanvas()*/);
        Shell.sInstance.stopService(new Intent(Shell.sInstance, Editor.PlayerService.class));
        }
        if (backPressCount < 7) {
        backPressCount++;
        } else {
            this.bonusunlocked = 1;
            if (backPressCount == 7) {
            setlock(21);
            while (this.n < 20) {
                    this.highscores[this.n + 10] = 2;
                    this.n++;
                }
            this.totalZonesUnlocked = 6;
            this.m_cheat = true;
            this.digatalalti = 1;
            SaveGame();
            backPressCount = 8;
            }
        }
        
        /*final String[] options = {"none", "_dec25", "_oct31", "_jan1", "_jan25", "_jun23"};
        if (backPressCount >= 7) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Shell.sInstance);
        builder.setTitle("Choose an event");

        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DeviceImage.result = options[which];
            }
        });
        builder.show();
        }*/
        }

        //if (this.m_cheat) {
        if (keycode == KeyEvent.KEYCODE_BACK && backPressCount == 8 && this.LoopModeWanted == 1 /*&& !demoStarted*/) {
                this.rings += 50;
                this.LoopModeWanted = 14;
        }
        this.keypressed = keycode;
        //if (keycode == KeyEvent.KEYCODE_BACK && backPressCount == 7) {
        if (keycode == cheat[this.cheatptr]) {
            this.cheatptr++;
            if (this.cheatptr < cheat.length && cheat[this.cheatptr] == 0) {
                this.cheatptr = 0;
                setlock(21);
                this.n = 0;
                while (this.n < 20) {
                    this.highscores[this.n + 10] = 2;
                    this.n++;
                }
                this.bonusunlocked = 1;
                this.m_cheat = true;
                this.digatalalti = 1;
            }
        } else {
            this.cheatptr = 0;
        }
        if (keycode == cheat2[this.cheatptr2]) {
        //if (keycode == KeyEvent.KEYCODE_BACK && backPressCount == 7) {
            this.cheatptr2++;
            if (this.cheatptr2 < cheat2.length && cheat2[this.cheatptr2] == 0) {
                this.cheatptr2 = 0;
                this.cheatactive[5] = 1 - this.cheatactive[5];
            }
        } else {
            this.cheatptr2 = 0;
        }
        if (this.cheatactive[3] > 0 && keycode == GFX_ANTENNA_BASE) {
            language++;
            if (language == 7) {
                language = 1;
            }
            initLanguage(language);
        }
        if (keycode == 1) {
            processSK1(this.sk1);
            return true;
        }
        if (keycode == 2) {
            processSK2(this.sk2);
            return true;
        }
        return processkey(1, keycode);
        //return super.onKeyDown(keycode, event);
    }

    public boolean onKeyUp(int keycode, KeyEvent event) {
        super.onKeyUp(keycode, event);
        this.lastpressed = 0;
        processkey(0, keycode);
        if (LoopMode != 10 && LoopMode != 11 && LoopMode != 19) {
            this.MenuTimer = 0;
        }
        if (keycode == 8 || keycode == 10) {
            this.KEY_RELEASED = true;
        }
        return true;
    }

    boolean processkey(int k, int keycode) {
        if (keycode == GFX_ZONE_3_ENEMY_A) {
            this.key_fire = k;
        }
        if (keycode == 17) {
            this.key_star = k;
            return true;
        }
        if (keycode == 18) {
            this.key_hash = k;
            return true;
        }
        if (keycode == 9 || keycode == -1) {
            this.key_right = k;
            return true;
        }
        if (keycode == 11 || keycode == -3) {
            this.key_up = k;
            return true;
        }
        if (keycode == 13 || keycode == -4) {
            this.key_down = k;
            return true;
        }
        if (keycode == 15 || keycode == -2) {
            this.key_left = k;
            return true;
        }
        if (keycode == 12 || keycode == -5) {
            this.key_fire = k;
            return true;
        }
        if (keycode == 8) {
            this.key_1 = k;
            return true;
        }
        if (keycode == 10) {
            this.key_3 = k;
            return true;
        }
        this.key_others = k;
        return true;
    }

    private void ProcessThirdSplash() {
        if (this.FirstFrame == 1) {
            this.splashdone = 1;
            this.FirstFrame = 0;
            this.MenuTimer = 280;
            setsoftkeys(0, 0);
            this.MenuTimer = 10;
            this.musicwanted = 1;
            this.keypressed = 0;
        }
        if (this.MenuTimer == 0) {
            if (this.key_up == 1 || this.key_down == 1 || this.key_left == 1 || this.key_right == 1 || this.key_fire == 1 || this.key_func1 == 1 || this.key_func2 == 1 || this.key_back == 1 || this.key_star == 1 || this.key_hash == 1 || this.key_1 == 1 || this.key_2 == 1 || this.key_3 == 1 || this.key_others == 1) {
                this.key_up = 0;
                this.key_down = 0;
                this.key_left = 0;
                this.key_right = 0;
                this.key_fire = 0;
                this.key_func1 = 0;
                this.key_func2 = 0;
                this.key_back = 0;
                this.key_star = 0;
                this.key_hash = 0;
                this.key_1 = 0;
                this.key_2 = 0;
                this.key_3 = 0;
                this.key_others = 0;
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessSecondSplash() {
        if (this.FirstFrame == 1) {
            this.FirstFrame++;
            this.MenuTimer = 280;
            this.PubPic = new DeviceImage(196620);
            OnlineIn = null;
            Online = false;
            /*this.realmap = new byte[this.TOTMAPBINSIZE];
            try {
                InputStream lin = this.m_res.openRawResource(R.raw.class.getField("map").getInt(null));
                DataInputStream ldin = new DataInputStream(lin);
                ldin.readFully(this.realmap, 0, this.TOTMAPBINSIZE);
                ldin.close();
            } catch (Exception e) {
            }*/
            loadMapFromAssets(true, null);
        } else if (this.FirstFrame == 2) {
            if (this.ZsPic == null) {
                this.ZsPic = new DeviceImage(196622);
            }
            if (this.ZrdPic == null) {
                this.ZrdPic = new DeviceImage(196621);
            }
            if (ExtrasPic == null) ExtrasPic = new DeviceImage[21];
            for (int i = 0; i < ExtrasPic.length; i++) {
            if (this.ExtrasPic[i] == null) {
                this.ExtrasPic[i] = new DeviceImage("ExtrasPic/" + 196617);
            }
            }
            if (this.SpritePic == null) {
            /*if (DemoFurikan) {
            shadow = true;
            sonic = false;
        this.SpritePic = new DeviceImage("shadow");
        } else {*/
                this.SpritePic = new DeviceImage(196619);
                //}
            }
            this.FirstFrame = 0;
        }
        this.MenuTimer -= 4;
        if (this.MenuTimer <= 0) {
            this.LoopModeWanted = 19;
        }
    }

    private void ProcessSecondSplashB() {
        if (this.FirstFrame == 1) {
            this.FirstFrame++;
            this.MenuTimer = 280;
            this.DevPic2 = new DeviceImage(196615);
            System.gc();
            setsoftkeys(0, 0);
        } else if (this.FirstFrame == 2) {
            this.n = 0;
            while (this.n < 21) {
                if (this.BlockPics[this.n] == null) {
                    this.BlockPics[this.n] = new DeviceImage("BlockPics/" + (196608 + (this.n < 3 ? 0 : n < 6 ? 1 : n < 9 ? 2 : n < 12 ? 3 : n < 15 ? 4 : n < 18 ? 5 : 5)));
                }
                this.n++;
            }
            this.FirstFrame++;
        } else if (this.FirstFrame == 3) {
            this.n = 0;
            while (this.n < 21) {
                if (this.StripPics[this.n] == null) {
                    this.StripPics[this.n] = new DeviceImage("StripPics/" + (131073 + ((this.n < 3 ? 0 : n < 6 ? 1 : n < 9 ? 2 : n < 12 ? 3 : n < 15 ? 4 : n < 18 ? 5 : 5) * 3)));
                }
                this.n++;
            }
            this.FirstFrame++;
        } else if (this.FirstFrame == 4) {
            this.n = 0;
            while (this.n < 21) {
                if (this.EdgePics[this.n] == null) {
                    this.EdgePics[this.n] = new DeviceImage("EdgePics/" + (131072 + ((this.n < 3 ? 0 : n < 6 ? 1 : n < 9 ? 2 : n < 12 ? 3 : n < 15 ? 4 : n < 18 ? 5 : 5) * 3)));
                }
                this.n++;
            }
            this.FirstFrame++;
        } else if (this.FirstFrame == 5) {
            this.n = 0;
            while (this.n < 21) {
                if (this.TilesPics[this.n] == null) {
                    this.TilesPics[this.n] = new DeviceImage("TilesPics/" + (131074 + ((this.n < 3 ? 0 : n < 6 ? 1 : n < 9 ? 2 : n < 12 ? 3 : n < 15 ? 4 : n < 18 ? 5 : 5) * 3)));
                }
                this.n++;
            }
            this.FirstFrame = 0;
        }
        this.MenuTimer -= 4;
        if (this.MenuTimer <= 0) {
            this.LoopModeWanted = 17;
        }
    }

    private void ProcessFirstSplash() {
        if (this.FirstFrame == 1) {
            this.FirstFrame++;
            this.MenuTimer = 320;
            setsoftkeys(0, 0);
            return;
        }
        if (this.FirstFrame == 2) {
            try {
                this.touchpad[0] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                this.touchpad[1] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                this.touchpad[2] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                this.touchpad[3] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                this.touchpad[4] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                this.touchpad[5] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
                //this.touchpad[6] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_2.png"));
                //this.touchpad[7] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_3.png"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.FirstFrame++;
        } else if (this.FirstFrame == 3) {
            this.FirstFrame++;
        } else if (this.FirstFrame == 4) {
            this.roomsize[0] = 288;
            this.roomsize[1] = 324;
            this.roomsize[2] = 366;
            this.roomsize[3] = 326;
            this.roomsize[4] = 342;
            this.roomsize[5] = 382;
            this.roomsize[6] = 334;
            this.roomsize[7] = 398;
            this.roomsize[8] = 398;
            this.roomsize[9] = 404;
            this.roomsize[10] = 364;
            this.roomsize[11] = 458;
            this.roomsize[12] = 492;
            this.roomsize[13] = 464;
            this.roomsize[14] = 452;
            this.roomsize[15] = 444;
            this.roomsize[16] = 460;
            this.roomsize[17] = 480;
            this.roomsize[18] = 420;
            this.roomsize[19] = 588;
            this.roomsize[20] = 674;
            int off = 0;
            int s = 0;
            for (int x = 0; x < 21; x++) {
                this.roomindex[x] = off;
                off = (int) (this.roomsize[x] + off);
                if (this.roomsize[x] > s) {
                    s = this.roomsize[x];
                }
            }
            this.TOTMAPBINSIZE = off;
            this.FirstFrame++;
        } else if (this.FirstFrame == 5) {
            this.FirstFrame++;
        } else if (this.FirstFrame == 6) {
            if (this.TitlePic == null) {
                this.TitlePic = new DeviceImage(this.m_res, "tiltle");
            }
            if (this.DiamondPic == null) {
                this.DiamondPic = new DeviceImage(196616);
            }
            if (this.MenuPic == null) {
                this.MenuPic = this.TitlePic;
            }
            if (this.PausePic == null) {
                this.PausePic = this.TitlePic;
            }
            if (this.demoMode.equals("1") || (this.demoMode.equals("2") && vodaDemo.equals("1"))) {
                try {
                    this.demoSplashImage = new DeviceImage(196614);
                } catch (Exception e2) {
                }
            }
            try {
                //this.m_ZoneSelectBg = BitmapFactory.decodeResource(this.m_res, R.drawable.class.getField("select_updn_360_640").getInt(null), this.m_Option);
                int resId = Shell.sInstance.getResources().getIdentifier("select_updn_360_640", "drawable", Shell.sInstance.getPackageName());
                this.m_ZoneSelectBg = BitmapFactory.decodeResource(this.m_res, resId, this.m_Option);
            } catch (Exception e3) {
                e3.printStackTrace();
            }
            this.FirstFrame = 0;
        }
        this.MenuTimer -= 4;
        if (this.MenuTimer <= 0) {
            this.LoopModeWanted = 11;
        }
    }

    private void DoMenu(int ptr, int menusize) {
        this.res = -1;
        if (this.MenuTimer == 0) {
            if (this.slideyoff == 0 && this.slideyoff == 0 && this.key_fire == 1) {
                this.key_fire = 0;
                this.res = this.menu[ptr];
                this.MenuTimer = 5;
                this.lastpressed = 1;
                this.bored = 0;
                flashTimer = 10;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void MenuWait() {
        this.res = -1;
        if (this.MenuTimer == 0) {
            if (this.slideyoff == 0 && this.slideyoff == 0 && this.key_fire == 1) {
                this.key_fire = 0;
                this.res = 1;
                this.MenuTimer = 5;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void setsoftkeys(int s1, int s2) {
        this.sk1 = s1;
        this.sk2 = s2;
        this.skupdate = 1;
    }

    private void ProcessMainMenu() {
        this.demoStarted = false;
        currentLine = 0;
        if (OnlineIn != null) {
        OnlineIn = null;
        BlockPics = BlockPicsBackup;
        EdgePics = EdgePicsBackup;
        ExtrasPic = ExtrasPicBackup;
        StripPics = StripPicsBackup;
        TilesPics = TilesPicsBackup;
        loadMapFromAssets(true, null);
        }
        Online = false;
        if (this.FirstFrame == 1) {
            if (!Paying.isPayed(0)) {
                this.highscores[10] = 1;
                this.highscores[11] = 0;
                this.highscores[12] = 0;
                resetscores();
            }
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            this.musicwanted = 1;
            this.bored = 0;
            this.slidex = -this.GameWidth;
            this.menu[2] = 0;
            BigFont = true;
            RegularFont = true;
            FontAdjust = true;
            helpAdjust = false;
            FontRightAdjust = false;
        }
        DoMenu(2, this.cheatactive[0] + 6 + this.incUpsell + 1/*this.incDemo*/);
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        if (DemoFurikan) {
        //int bu = 0;
        if (this.res == 0) {
        ProcessDemoStart();
        }
        /*if (this.bonusunlocked == 1) {
        bu = 1;*/
        if (this.res == 1) {
        this.LoopModeWanted = 35;
        }
        //}
        if (this.res == 2) {
            this.LoopModeWanted = 15;
        }
        if (this.res == 3) {
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            this.textheading = 6;
            inittext(localText[GFX_ZONE_5_ENEMY_A]);
            this.LoopModeWanted = 16;
        }
        if (this.res == 4) {
            this.LoopModeWanted = 8;
        }
        return;
        }
        if (multip) {
        if (this.res == 0) {
        initgame();
        BigFont = false;
        }
        if (this.res == 1) {
        ProcessDemoStart();
        BigFont = false;
        }
        return;
        }
        if (this.bonusunlocked == 1 || backPressCount >= 7) multiplayerh = 0;
        if (this.demoMode.equals("0") && this.res == 0) {
            /*if (this.bonusunlocked == 1 || backPressCount >= 7){*/
                //initgame();
                this.LoopModeWanted = 41 /*5*/;
            /*} else {
                shadowstory = false;
                initgame();
                this.LoopModeWanted = 5;
            }*/
        }
        if (this.res == 1) {
        //this.LoopModeWanted = 31;
        ProcessDemoStart();
        RegularFont = false;
        }
        if (this.res == 4 + multiplayerh) {
        BigFont = true;
        RegularFont = true;
        FontAdjust = true;
        this.LoopModeWanted = 28;
        }
        if (this.res == 5 + multiplayerh) {
        event = true;
        BigFont = true;
        RegularFont = true;
        FontAdjust = true;
        this.LoopModeWanted = 36;
        }
        
        if (multiplayerh == 0 && this.res == 3) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
            getLocalIpAddress(new Game_Draw.OnIPSelectedListener() {
        @Override
        public void onIPSelected(InetAddress selectedIP) {
            if (selectedIP != null) {
               InetAddress localAddress = selectedIP; // Define o valor de localAddress
               String ipMessage = "Your device IP: " + localAddress.getHostAddress();
               searchNetwork(ipMessage);
            } else {
            }
        }
    });
    }
        });
        }
        
        if (this.res == sp + 4 + multiplayerh) {
            this.hsx = 0;
            this.hsy = 0;
            BigFont = true;
            RegularFont = true;
            FontAdjust = true;
            this.LoopModeWanted = 37;
        }
        if (this.res == sp + 5 + multiplayerh) {
            BigFont = true;
            RegularFont = true;
            FontAdjust = true;
            this.LoopModeWanted = 15;
        }
        if (this.res == sp + 6 + multiplayerh) {
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            this.textheading = 5;
            inittext(localText[GFX_ZONE_3_ENEMY_B]);
            BigFont = false;
            RegularFont = false;
            FontAdjust = false;
            helpAdjust = true;
            this.LoopModeWanted = 16;
        }
        if (this.res == sp + 7 + multiplayerh) {
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            this.textheading = 6;
            inittext(localText[GFX_ZONE_5_ENEMY_A]);
            BigFont = false;
            RegularFont = false;
            FontAdjust = true;
            helpAdjust = false;
            this.LoopModeWanted = 16;
        }
        if (this.res == sp + 8 + multiplayerh) {
        editor = new Editor();
        }
        if (this.res == sp + 9 + multiplayerh) {
            BigFont = true;
            RegularFont = true;
            FontAdjust = true;
            if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
                if (vodaDemo.equals("1")) {
                    processEndDemo();
                    this.LoopModeWanted = 8;
                    return;
                } else {
                    processEndDemo();
                    this.LoopModeWanted = 8;
                    return;
                }
            }
            this.LoopModeWanted = 8;
        }
        if (multiplayerh == 0 && this.res == sp + 10 + multiplayerh) {
        this.LoopModeWanted = 39;
        }
        
        //if (this.bonusunlocked == 1) {
        sp = 2;
        if (this.res == 2) {
        BigFont = true;
        RegularFont = true;
        FontAdjust = true;
        this.LoopModeWanted = 35;
        }
        //}
    }
    
    public Editor getEditor() {
    return editor;
    }
    
    public void ProcessSelect() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.musicwanted = 1;
            this.slidex = this.GameWidth;
            this.menu[35] = 0;
        }
    DoMenu(35, 3);
    if (res >= 0) {
    event = false;
    }
    if (res == 2) {
        shadow = true;
        sonic = false;
        tails = false;
        character = "shadow";
        this.SpritePic = new DeviceImage("shadow");
        this.LoopModeWanted = 2;
    }
    if (res == 1 && !DemoFurikan && this.bonusunlocked == 1 || res == 1 && DemoFurikan && backPressCount >= 7) {
        tails = true;
        sonic = false;
        shadow = false;
        character = "tails";
        this.SpritePic = new DeviceImage("tails");
        this.LoopModeWanted = 2;
        }
        if (res == 0) {
        character = "sonic";
        tails = false;
        sonic = true;
        shadow = false;
        this.SpritePic = new DeviceImage(196619);
        this.LoopModeWanted = 2;
        }
        if (this.key_back == 1) {
        if (DemoFurikan) {
        shadow = true;
        sonic = false;
        tails = false;
        this.SpritePic = new DeviceImage("shadow");
        this.LoopModeWanted = 2;
        return;
        }
                if (sonic) this.SpritePic = new DeviceImage(196619);
                this.LoopModeWanted = 2;
                return;
            }
    }
    public void DrawSelect() {
    Cls(1);
        this.drawY = 30;
        bluebox(148);
        /*String[] titles = {
        "CHARACTER SELECT",     // 1 - Inglês
        "SÉLECTION DE PERSONNAGE",    // 2 - Francês
        "SELEZIONE DEL CARATTERE",     // 3 - Italiano
        "CHARAKTERAUSWAHL",      // 4 - Alemão (Deutsch)
        "SELECCION DE PERSONAJE", // 5 - Espanhol
        "SELEÇÃO DE PERSONAGEM",   // 6 - Português
        "SELEÇÃO DE PERSONAGEM",   // 7 - Português Brasileiro
        "キャラクター選択",   // 8 - Japonês
        "角色选择"         // 9 - Chinês Simplificado
    };*/
        Tad(localText[222]/*titles[this.highscores[3] -1]*/, Color_White, false);
        Gap();
        Mad(this.menu[35] == 0, localText[227], Color.BLUE, false);
        if (!DemoFurikan && this.bonusunlocked == 1 || DemoFurikan && backPressCount >= 7) {
        Mad(this.menu[35] == 1, localText[229], Color.YELLOW, false);
        } else {
        Mad(this.menu[35] == 1, "ฅ⁠^⁠•⁠ﻌ⁠•⁠^⁠ฅ", Color.YELLOW, false);
        }
        Mad(this.menu[35] == 2, localText[228], Color.GRAY, false);
    }
    
    public void ProcessHS() {
    if (this.FirstFrame == 1) {
        this.MenuTimer = 10;
        this.FirstFrame = 0;
        setsoftkeys(1, 2);
        this.musicwanted = 1;
        this.slidex = this.GameWidth;
        this.menu[37] = 0;
    }
    DoMenu(37, 2);
    if (res == 0) {
        this.LoopModeWanted = 4;
    }
    if (res == 1) {
        this.LoopModeWanted = 38;
        }
        if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
    }
    
    public void ProcessUNUSED() {
    if (this.FirstFrame == 1) {
        this.MenuTimer = 10;
        this.FirstFrame = 0;
        setsoftkeys(1, 2);
        this.musicwanted = 1;
        this.slidex = this.GameWidth;
        this.menu[38] = 0;
    }
    DoMenu(38, 2);
    if (res == 0) {
        this.LoopModeWanted = 29;
    }
    if (res == 1) {
        this.LoopModeWanted = 33;
        }
        if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
    }
    
    public void DrawUNUSED() {
    Cls(1);
        this.drawY = 30;
        bluebox(113);
        Tad("UNUSED"/*titles[this.highscores[3] -1]*/, Color_White, false);
        Gap();
        Mad(this.menu[39] == 0, "ROTATION", Color_White, false);
        Mad(this.menu[39] == 1, "NOKIASKU", Color_White, false);
    }
    
    public void DrawHS() {
        Cls(1);
        this.drawY = 30;
        bluebox(113);
        Tad(localText[3]/*titles[this.highscores[3] -1]*/, Color_White, false);
        Gap();
        Mad(this.menu[37] == 0, localText[54], Color_White, false);
        Mad(this.menu[37] == 1, localText[221], Color_White, false);
    }
    
    public void ProcessHS2() {
    if (this.FirstFrame == 1) {
        this.MenuTimer = 10;
        this.FirstFrame = 0;
        setsoftkeys(1, 2);
        this.musicwanted = 1;
        this.slidey = this.GameHeight;
        this.menu[38] = 0;
    }
        if (this.key_back == 1) {
                this.LoopModeWanted = 37;
                return;
            }
    }
    public void DrawHS2() {
    Cls(1);
        this.drawY = 30;
        if (speedtime > 0 && speedtime2 == 0) {
            bluebox(82);
        } else if (speedtime2 > 0 && speedtime > 0) {
            bluebox(117);
        } else {
            bluebox(82);
        }
        Tad(localText[3], Color_White, true);
        Gap();
        if (speedtime > 0) {
            int minutes = Math.abs(speedtime) / 60;
            int seconds = Math.abs(speedtime) % 60;
            String timeFormatted = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds);
            Tad(localText[221] + ": "/*"SPEEDRUN TIME: "*/ + timeFormatted, Color_White, true);
        }
        if (speedtime2 > 0) {
            int minutes2 = Math.abs(speedtime2) / 60;
            int seconds2 = Math.abs(speedtime2) % 60;
            String timeFormatted2 = minutes2 + ":" + (seconds2 < 10 ? "0" + seconds2 : seconds2);
            Tad(localText[232] + ": "/*"SPEEDRUN TIME (BONUS COMPLETED): "*/ + timeFormatted2, Color_White, true);
        }
        if (speedtime <= 0 && speedtime2 <= 0) {
            Tad(localText[221] + ": " + "34:00", Color_White, true);
        }
    }
    
    private void launchBrowser(final String demoURL2) {
    if (!notification) createNotificationChannel();
    notification = true;
    Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            final WebView webView = new WebView(Shell.sInstance);

            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setDomStorageEnabled(true);

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
            });
            
            webView.setWebChromeClient(new WebChromeClient() {

    // Android 5.0+
    @Override
    public boolean onShowFileChooser(WebView webView,
                                     ValueCallback<Uri[]> filePathCallback,
                                     FileChooserParams fileChooserParams) {
        if (Shell.sInstance.mFilePathCallback != null) {
            Shell.sInstance.mFilePathCallback.onReceiveValue(null);
        }
        Shell.sInstance.mFilePathCallback = filePathCallback;
        Intent intent = fileChooserParams.createIntent();
        try {
            Shell.sInstance.startActivityForResult(intent, 1);
        } catch (ActivityNotFoundException e) {
            Shell.sInstance.mFilePathCallback = null;
            Toast.makeText(Shell.sInstance, "Unable to open file picker", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    // Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser(uploadMsg, "*/*");
    }

    // Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        openFileChooser(uploadMsg, acceptType, null);
    }

    // Android 4.1+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        Shell.sInstance.mUploadMessage = uploadMsg;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        try {
            Shell.sInstance.startActivityForResult(Intent.createChooser(intent, "Select a file"), 1);
        } catch (ActivityNotFoundException e) {
            Shell.sInstance.mUploadMessage = null;
            Toast.makeText(Shell.sInstance, "Unable to open file picker", Toast.LENGTH_SHORT).show();
        }
    }
});

            if (demoURL2 != null && demoURL2.startsWith("http")) {
                webView.loadUrl(demoURL2);
            }

webView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(final String url, final String userAgent, String contentDisposition, String mimeType, long contentLength) {
        if (browsers == 1 || (url.contains("decoded_map") && url.contains(".txt")) || (url.contains("map") && url.contains(".bin"))) {

            final int notifId = 1001;

            Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showNotification("Download started", "Downloading file: " + url, notifId);
                }
            });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    InputStream is = null;
                    ByteArrayOutputStream baos = null;
                    try {
                        URL downloadUrl = new URL(url);
                        connection = (HttpURLConnection) downloadUrl.openConnection();
                        connection.setRequestProperty("User-Agent", userAgent);
                        connection.setInstanceFollowRedirects(true);
                        connection.connect();
                        final int s = connection.getResponseCode();
                        final String s2 = connection.getHeaderField("Location");
                        Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Shell.sInstance, "Response code: " + s, Toast.LENGTH_SHORT).show();
                    //Toast.makeText(Shell.sInstance, "Location: " + s2, Toast.LENGTH_SHORT).show();
                }
            });
                        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                            is = connection.getInputStream();
                            baos = new ByteArrayOutputStream();

                            byte[] buffer = new byte[8192];
                            int bytesRead;
                            while ((bytesRead = is.read(buffer)) != -1) {
                                baos.write(buffer, 0, bytesRead);
                            }

                            byte[] data = baos.toByteArray();
                            OnlineIn = new ByteArrayInputStream(data);
                            ByteArrayInputStream On2 = new ByteArrayInputStream(data);
                            Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showNotification("Download finished", "File downloaded successfully: " + url, notifId);
                    Toast.makeText(Shell.sInstance, "Download finished", Toast.LENGTH_SHORT).show();
                }
            });
                        OnlineIn.mark(OnlineIn.available());
                        if (browsers == 1 && (!url.contains("decoded_map.txt") || !url.contains("map.bin"))) {
                        String path = downloadUrl.getPath();
                        String fileName = path.substring(path.lastIndexOf('/') + 1);
                        if (Build.VERSION.SDK_INT > 28) {                        
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
                        values.put(MediaStore.Downloads.MIME_TYPE, "application/octet-stream");
                        values.put(MediaStore.Downloads.IS_PENDING, 1);
                        ContentResolver resolver = Shell.sInstance.getContentResolver();
                        Uri collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
                        Uri fileUri = resolver.insert(collection, values);
                        try (OutputStream out = resolver.openOutputStream(fileUri)) {
                             out.write(data);
                        }
                        values.clear();
                        values.put(MediaStore.Downloads.IS_PENDING, 0);
                        resolver.update(fileUri, values, null, null);
                        } else {
                        File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                        File outputFile = new File(downloadsDir, fileName);
                        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                        fos.write(data);
                        fos.flush();
                        } catch (IOException e) {
                          e.printStackTrace();
                        }
                        }
                        } else {
                        Online = true;
                        loadMapFromAssets(url.contains("decoded_map.txt"), On2);
                        }
                        } else {
                        Shell.sInstance.runOnUiThread(new Runnable() {
                        @Override
                public void run() {
                    showNotification("Download failed", "File not downloaded successfully: " + url, notifId);
                }
            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        Shell.sInstance.runOnUiThread(new Runnable() {
                        @Override
                public void run() {
                    showNotification("Download failed", "File not downloaded successfully: " + url, notifId);
                }
            });
                    } finally {
                        if (baos != null) {
                            try { baos.close(); } catch (IOException ignored) {}
                        }
                        if (is != null) {
                            try { is.close(); } catch (IOException ignored) {}
                        }
                        if (connection != null) {
                            connection.disconnect();
                        }
                    }
                }
            }).start();
        } else {
        Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
                public void run() {
                    Toast.makeText(Shell.sInstance, "File is not decoded_map.txt or map.bin", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
});
Shell.sInstance.setContentView(webView);
}
});
}

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
            CHANNEL_ID,
            "Download Notifications",
            NotificationManager.IMPORTANCE_LOW);
        channel.setDescription("Notifications for file downloads");

        NotificationManager manager = (NotificationManager) Shell.sInstance.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }
}

private void showNotification(String title, String text, int notificationId) {
    NotificationManager manager = (NotificationManager) Shell.sInstance.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder = new Notification.Builder(Shell.sInstance, CHANNEL_ID);
    } else {
        builder = new Notification.Builder(Shell.sInstance);
        builder.setPriority(Notification.PRIORITY_LOW);
    }

    builder.setContentTitle(title)
           .setContentText(text)
           .setSmallIcon(title.contains("finished") ? Shell.sInstance.getResources().getIdentifier("icon", "mipmap", Shell.sInstance.getPackageName()) : android.R.drawable.stat_sys_download)
           .setAutoCancel(true);

    manager.notify(notificationId, builder.build());
}

    private void ProcessFin() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            this.musicwanted = 109;
            setsoftkeys(1, 0);
        }
        MenuWait();
        if (this.res != -1) {
            LoopMode = 23;
            this.LoopModeWanted = 12;
        }
    }

    String getword() {
        String w = "";
        while (true) {
            char c = this.para[this.stringptr];
            if (c == GFX_SPINNER_POLE) {
                c = '\"';
            }
            this.stringptr++;
            if (c == '\n') {
                this.gapchar = c;
                return w;
            }
            if (this.stringptr == this.paralen) {
                String w2 = String.valueOf(w) + c;
                this.gapchar = 0;
                return w2;
            }
            w = String.valueOf(w) + c;
        }
    }

    void inittext(String s) {
        int totlen;
        this.modefrom = LoopMode;
        this.lineshown = 0;
        this.para = s.toCharArray();
        this.paralen = s.length();
        this.n = 0;
        while (this.n < GFX_SONIC_DEAD_FLIP) {
            this.stringlist[this.n] = "";
            this.n++;
        }
        this.numstrings = 0;
        this.stringptr = 0;
        if (this.paralen != 1) {
            int totlen2 = 0;
            while (true) {
                String word = getword();
                this.stringlist[this.numstrings] = word;
                int len = this.G.stringWidth(word);
                if (this.gapchar == 10) {
                    this.numstrings++;
                    totlen2 = 0;
                } else {
                    if (len > this.stringmax) {
                        char[] szWordArray = word.toCharArray();
                        this.nMultiplier = 0;
                        while (this.nMultiplier * this.stringmax < len) {
                            this.nMultiplier++;
                        }
                        for (int j = 0; j < this.nMultiplier; j++) {
                            this.szHalfWord = "";
                            for (int k = (szWordArray.length / this.nMultiplier) * j; k < (szWordArray.length / this.nMultiplier) * (j + 1); k++) {
                                this.szHalfWord = String.valueOf(this.szHalfWord) + szWordArray[k];
                            }
                            this.numstrings++;
                        }
                        this.numstrings--;
                        totlen = 0;
                    } else if (totlen2 + len > this.stringmax) {
                        this.numstrings++;
                        totlen = len;
                    } else {
                        totlen = totlen2 + len;
                    }
                    if (this.gapchar == 0) {
                        break;
                    }
                    String[] strArr = this.stringlist;
                    int i = this.numstrings;
                    strArr[i] = String.valueOf(strArr[i]) + " ";
                    totlen2 = totlen + (this.props[0] << 1) + 2;
                }
            }
            for (int i2 = 0; i2 < this.stringlist.length; i2++) {
                if (localText[GFX_ZONE_5_ENEMY_A] == s) {
                    System.out.println(String.valueOf(i2) + "," + this.stringlist[i2]);
                }
            }
        }
    }

    void setlock(int d) {
        for (int n = 0; n < 21; n++) {
            this.highscores[n + 10] = 0;
        }
        for (int n2 = 0; n2 < d; n2++) {
            this.highscores[n2 + 10] = 1;
        }
    }

    int getscore(int a, int p) {
        int offset = (((a * 3) + p) * 3) + 31;
        int b1 = this.highscores[offset + 0];
        int b2 = this.highscores[offset + 1];
        int b3 = this.highscores[offset + 2];
        int s = b1 + 128 + ((b2 + 128) << 8) + ((b3 + 128) << 16);
        return s - 10000;
    }

    void setscore(int a, int p, int s) {
        int offset = (((a * 3) + p) * 3) + 31;
        int s2 = s + 10000;
        int b1 = s2 & 255;
        int s3 = s2 >> 8;
        int b2 = s3 & 255;
        int b3 = (s3 >> 8) - 128;
        this.highscores[offset + 0] = (byte) (b1 - 128);
        this.highscores[offset + 1] = (byte) (b2 - 128);
        this.highscores[offset + 2] = (byte) b3;
    }

    void resetscores() {
        for (int n = 0; n < 21; n++) {
            setscore(n, 0, -300);
            setscore(n, 1, -200);
            setscore(n, 2, -100);
        }
    }

    static int getVolume() {
        if (volume == 0) {
            return GFX_SONIC_DEAD_FLIP;
        }
        if (volume == 1) {
            return GFX_ZONE_1_ENEMY_B;
        }
        if (volume == 2) {
            return 100;
        }
        return 0;
    }

    void incsound() {
        if (this.SoundAvailable == 1) {
            this.SoundOnOff++;
            if (this.SoundOnOff >= 2) {
                this.SoundOnOff = 0;
            }
        }
        if (this.SoundAvailable != 2) {
            if (this.SoundAvailable == 3) {
                if (this.SoundOnOff != 1 || volume > 1) {
                    this.SoundOnOff++;
                    if (this.SoundOnOff == 2) {
                        this.SoundOnOff = 0;
                    }
                    if (this.SoundOnOff == 3) {
                        this.SoundOnOff = 1;
                    }
                    volume = 0;
                } else {
                    volume++;
                }
            }
            if (this.SoundOnOff == 0) {
                this.musicwanted = -1;
                processSound(2, 0, 0);
            } else {
                processSound(2, 0, 0);
                this.musicwanted = 2;
                processSound(1, 2, 1);
                this.musicwanted = -3;
            }
            this.highscores[0] = (byte) this.SoundOnOff;
        }
    }

    void incmusic() {
        if (this.SoundAvailable != 1) {
            if (this.SoundAvailable == 2) {
                if (this.SoundOnOff == 2) {
                    this.SoundOnOff = 0;
                } else {
                    this.SoundOnOff = 2;
                }
            } else if (this.SoundAvailable == 3) {
                if (this.SoundOnOff != 2 || volume > 1) {
                    this.SoundOnOff++;
                    if (this.SoundOnOff == 1) {
                        this.SoundOnOff = 2;
                    }
                    if (this.SoundOnOff == 3) {
                        this.SoundOnOff = 0;
                    }
                    volume = 0;
                } else {
                    volume++;
                }
            }
            if (this.SoundOnOff == 0) {
                this.musicwanted = -1;
                processSound(2, 0, 0);
            } else if (LoopMode == 15) {
                this.musicwanted = 1;
            }
            System.out.println(" incmusic   " + volume);
            this.highscores[0] = (byte) this.SoundOnOff;
        }
    }
    
    private boolean hasNull(DeviceImage[] arr) {
    for (DeviceImage img : arr) {
        if (img == null) return true;
    }
    return false;
}

    private void ProcessOptions() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(0, 2);
            this.musicwanted = 1;
            this.slidex = this.GameWidth;
            this.menu[15] = 0;
        }
        int n = 0;
        if (this.SoundAvailable == 1 || this.SoundAvailable == 2) {
            n = 1;
        }
        if (this.SoundAvailable == 3) {
            n = 2;
        }
        DoMenu(15, n + 1 + 1 + 1/*this.incLang*/ + 1 + 1);
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        /*if (this.res == 0) {
        }*/
            if (n == 1) {
            if (this.res == 0) {
                if (this.SoundAvailable == 1) {
                    incsound();
                } else {
                    incmusic();
                }
                SaveGame();
                clearkeys();
                if (this.SoundAvailable == 1) {
                    this.musicwanted = -1;
                    processSound(1, -1, 1);
                }
            }
        } else if (n == 2) {
            if (this.res == 0) {
                incsound();
                SaveGame();
                clearkeys();
                this.musicwanted = -1;
                processSound(1, -1, 1);
            }
            if (this.res == 1) {
                incmusic();
                SaveGame();
                clearkeys();
            }
        }
        if (this.res == n +2) {
            this.VibraOnOff = 1 - this.VibraOnOff;
            this.vibrawanted = 2;
            this.highscores[1] = (byte) this.VibraOnOff;
            SaveGame();
            clearkeys();
        }
        if (this.res == n + 1 +2) {
            this.TouchpadVisibleOnOff = 1 - this.TouchpadVisibleOnOff;
        }
        if (this.res == n + 4) {
        this.LoopModeWanted = 42;
        }
        
        if (this.incLang == 1 && this.res == n + 1 + 1 +2 +1) {
            if (this.multiLang.equals("1")) {
                this.LoopModeWanted = 3;
                /*this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showLanguageDialog();
            }
        });*/
            } else {
                initText("lang");
            }
        }
        if (this.res == n + 1 + 1/*this.incLang*/ + 1 +2 +1) {
            this.LoopModeWanted = 21;
            /*this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showOptions();
            }
        });*/
        }
        /*if (this.res == n + 4) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
            getLocalIpAddress(new Game_Draw.OnIPSelectedListener() {
        @Override
        public void onIPSelected(InetAddress selectedIP) {
            if (selectedIP != null) {
               InetAddress localAddress = selectedIP; // Define o valor de localAddress
               String ipMessage = "Your device IP: " + localAddress.getHostAddress();
               searchNetwork(ipMessage);
            } else {
            }
        }
    });
    }
        });
        }*/
        /*if (this.res == n + 5) {
        if (FPSV == 16) {
        FPSV = 24;
        } else if (FPSV == 24) {
        FPSV = 30;
        } else if (FPSV == 30) {
        FPSV = 32;
        } else if (FPSV == 32) {
        FPSV = 48;
        } else if (FPSV == 48) {
        FPSV = 50;
        } else if (FPSV == 50) {
        FPSV = 60;
        } else if (FPSV == 60) {
        FPSV = 16;
        }
        if (FPSV == 16) {
        MIMIMUM_FRAME_TIME = (long) (FPSV * 3.75);
        } else {
        MIMIMUM_FRAME_TIME = (long) (60/(FPSV / 16));
        }
        }*/
        if (this.res == n + 5 -5) {
        this.LoopModeWanted = 17;
        soundtrack = true;
        }
        
        /*if (this.res == n + 7) {
        if (crt && crt2 != 1) {
        crt2 = 1;
        } else if (crt2 == 1) {
        crt = false;
        crt2 = 0;
        } else {
        crt = true;
        crt2 = 0;
        }
        CRTEnabled();
        }*/
        
        /*if (this.res == n + 8) {
        Graphics.customfonts = Graphics.customfonts ? false : true;
        }*/
        
        /*if ((this.bonusunlocked == 1 || backPressCount >= 7) && this.res == n + 8) {
        alt = alt ? false : true;
        
        if (sonic) SpritePic = new DeviceImage(196619);
        else if (shadow) SpritePic = new DeviceImage("shadow");
        DiamondPic = new DeviceImage(196616);
        ZsPic = new DeviceImage(196622);
        ZrdPic = new DeviceImage(196621);
      
        new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
        for (int i2 = 0; i2 < 2; i2++) {
        if (CSPics[i].getId() == 65555 + i2) {
        CSPics[i] = new DeviceImage(65555 + i2);
        }
        }
        }
        int i2 = 0;
        for (int i = 0; i < 21; i++) {
        BlockPics[i] = new DeviceImage("BlockPics/" + (196608 + i2));
        StripPics[i] = new DeviceImage("StripPics/" + (131073 + (i2 * 3)));
        TilesPics[i] = new DeviceImage("TilesPics/" + (131074 + (i2 * 3)));
        EdgePics[i] = new DeviceImage("EdgePics/" + (131072 + (i2 * 3)));
        if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14) i2++;
        }
        }
        }).start();
        while (hasNull(BlockPics) || hasNull(StripPics) || hasNull(TilesPics) || hasNull(EdgePics)) {
        }
        }
        
        if (this.res == n + 6 -2) {
        customtouchpad = customtouchpad ? false : true;
        try {
        if (customtouchpad) {
        this.touchpad[0] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_0_s40.png"));
        this.touchpad[1] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("leftkey_1_s40.png"));
        this.touchpad[2] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("ok_key_0_s40.png"));
        this.touchpad[3] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("ok_key_1_s40.png"));
        this.touchpad[4] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("pause_0_s40.png"));
        this.touchpad[5] = BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("pause_1_s40.png"));
        } else {
        this.touchpad[0] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[1] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("leftkey_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[2] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[3] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("ok_key_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[4] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_0", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        this.touchpad[5] = BitmapFactory.decodeResource(this.m_res, Shell.sInstance.getResources().getIdentifier("pause_1", "drawable", Shell.sInstance.getPackageName()), this.m_Option);
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        }*/
        
        if (this.res == n + 4 -3) {
        //this.LoopModeWanted = 36;
        this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
        /*String[] titles = {
        "SOUND TEST",     // 1 - Inglês
        "TEST SONORE",    // 2 - Francês
        "TEST SUONO",     // 3 - Italiano
        "KLANGTEST",      // 4 - Alemão (Deutsch)
        "PRUEBA DE SONIDO", // 5 - Espanhol
        "TESTE DE SOM",   // 6 - Português
        "TESTE DE SOM",   // 7 - Português Brasileiro
        "サウンドテスト",   // 8 - Japonês
        "声音测试"         // 9 - Chinês Simplificado
    };*/
    AlertDialog.Builder builder = new AlertDialog.Builder(Game_Draw.this.activity);
builder.setTitle(Game_Draw.localText[221]/*titles[this.highscores[3] -1]*/);

String[] q;
final boolean p = JumpPro || Fever;

if (p) {
q = getTuneNames(Shell.sInstance);
} else {
q = new String[22];
}

final String[] tuneNames = q;

if (!p) {
    for (int i = 0; i < 22; i++) {
        tuneNames[i] = "tune" + String.format("%02d", i + 1);
    }
}

builder.setSingleChoiceItems(tuneNames, -1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        String selectedTune = tuneNames[which]; // Obtém a música correspondente à seleção

        if (!mediaPlayer.isPlaying()) {
            playSound(selectedTune, p);
            Game_Draw.this.musicwanted = -1;
            DeviceSound.releaseAll();
        } else {
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
    }
});

// Listener para parar o MediaPlayer quando o diálogo for fechado
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            Game_Draw.ResumeSound = DeviceSound.getLastSound();
            Game_Draw.this.musicwanted = 1;
        }
    }
});
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

builder.show();
}
});
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 2;
            SaveGame();
        }
    }

private void playSound(String soundName, boolean p) {
    if (mediaPlayer != null) {
        mediaPlayer.release(); // Libera o MediaPlayer antes de iniciar um novo som
    }
    mediaPlayer = new MediaPlayer();
    if (p) {
    try {
    AssetFileDescriptor afd = activity.getAssets().openFd(soundName);
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
        } catch (IOException e) {
        }
    } else {
    int resId = getResources().getIdentifier(soundName, "raw", Shell.sInstance.getPackageName());
    if (resId != 0) {
        mediaPlayer = MediaPlayer.create(Shell.sInstance, resId);
        mediaPlayer.start();
        }
    }
}

    public static String[] getTuneNames(Context context) {
        AssetManager assetManager = context.getAssets();
        String path = "";
        
        if (JumpPro) {
            path = "Pro";
        } else if (Fever) {
            path = "Fever";
        }

        List<String> tuneNames = new ArrayList<>();

        try {
            String[] files = assetManager.list(path);
            if (files != null) {
                for (String file : files) {
                    if (file.endsWith(".m4a")) {
                        tuneNames.add(path + "/" + file);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return tuneNames.toArray(new String[0]);
    }

    public void searchNetwork(String ip) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
        builder.setTitle("Choose Mode. \n" + ip)
               .setMessage("Do you want to be the receiver or the client?")
               .setPositiveButton("Server", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       iniciarReceptor();
                   }
               })
               .setNegativeButton("Client", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       iniciarCliente();
                   }
               })
               .show();
    }
    
    public interface OnIPSelectedListener {
        void onIPSelected(InetAddress selectedIP);
    }

    public boolean isPrivateIP(InetAddress inetAddress) {
    String ip = inetAddress.getHostAddress();
    return ip.startsWith("10.") || ip.startsWith("172.") || ip.startsWith("192.168");
}

    // Método para obter os IPs e exibir o AlertDialog
    public void getLocalIpAddress(final OnIPSelectedListener listener) {
        final List<InetAddress> validIPs = new ArrayList<>();

        try {
            // Varrendo todas as interfaces de rede
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                            if (isPrivateIP(inetAddress)) {
    validIPs.add(inetAddress);
}
                    }
            }
        } catch (SocketException ex) {
            ex.printStackTrace();
        }

        // Se não houver IPs válidos, invoca o callback com null
        if (validIPs.isEmpty()) {
            listener.onIPSelected(null);
            return;
        }

        // Converte lista de InetAddress para uma lista de String para exibição
        List<String> ipStrings = new ArrayList<>();
        for (InetAddress addr : validIPs) {
            ipStrings.add(addr.getHostAddress());
        }

        // Adapter para o AlertDialog
        ArrayAdapter<String> adapter = new ArrayAdapter<>(Shell.sInstance, android.R.layout.simple_list_item_1, ipStrings);

        // Cria o AlertDialog com a lista de IPs e exibe
        new AlertDialog.Builder(Shell.sInstance)
            .setTitle("Select IP")
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Retorna o IP selecionado ao callback
                    listener.onIPSelected(validIPs.get(which));
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) { 
        listener.onIPSelected(null);
        }
        })
            .show();
    }

    private void iniciarReceptor() {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(12345);
                final Socket socket = serverSocket.accept(); // Aguarda a conexão do cliente

                // Após aceitar a conexão, atualiza a UI na thread principal
                Shell.sInstance.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //Game_Draw.this.GameHeight = Game_Draw.this.GameHeight / 2;
                        //Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
                        GameTask gameTask = new GameTask(Shell.sInstance, socket, true, G); // Receptor
                        multip = true;
                    }
                });

                serverSocket.close(); // Fecha o servidor após a conexão (opcional)
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

    private void iniciarCliente() {
    Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // Cria o AlertDialog com um EditText para o IP
            AlertDialog.Builder builder = new AlertDialog.Builder(Shell.sInstance);
            builder.setTitle("Enter the Receiver IP");

            // Cria o EditText para o IP
            final EditText input = new EditText(Shell.sInstance);
            input.setHint("Ex: 192.168.1.100");
            builder.setView(input);

            // Define os botões do AlertDialog
            builder.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    final String ipReceptor = input.getText().toString().trim();
                    if (ipReceptor.isEmpty()) {
                        Toast.makeText(Shell.sInstance, "IP cannot be empty", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // Conecta ao receptor em uma thread separada
                    ExecutorService executor = Executors.newSingleThreadExecutor();
                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                final Socket socket = new Socket(ipReceptor, 12345); // Conecta ao receptor

                                // Atualiza a UI na thread principal
                                Shell.sInstance.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //Game_Draw.this.GameHeight = Game_Draw.this.GameHeight / 2;
                                        //Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
                                        GameTask gameTask = new GameTask(Shell.sInstance, socket, false, G); // Cliente
                                        multip = true;
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                                
                                // Mostra erro na UI
                                Shell.sInstance.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(Shell.sInstance, "Erro ao conectar", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    });
                }
            });

            builder.setNegativeButton("Cancel", null); // Caso o usuário cancele
            builder.show(); // Exibe o AlertDialog
        }
    });
}

    private void ProcessHighScores() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(0, 2);
            this.musicwanted = 1;
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            if (this.key_up == 1 && this.hsy > 0) {
                this.hsy--;
                this.slidey = -this.GameHeight;
                this.MenuTimer = 10;
            }
            if (this.key_down == 1 && this.hsy < 6 && this.highscores[(this.hsy * 3) + 10 + this.hsx + 3] != 0) {
                this.hsy++;
                this.slidey = this.GameHeight;
                this.MenuTimer = 10;
            }
            if (this.key_left == 1 && this.hsx > 0) {
                this.hsx--;
                this.slidex = -this.GameWidth;
                this.MenuTimer = 10;
            }
            if (this.key_right == 1 && this.hsx < 2 && this.highscores[(this.hsy * 3) + 10 + this.hsx + 1] != 0) {
                this.hsx++;
                this.slidex = this.GameWidth;
                this.MenuTimer = 10;
            }
            if (this.key_back == 1) {
                this.LoopModeWanted = 37;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessResults() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 0);
            this.musicwanted = 1;
            this.slidey = this.GameHeight;
        }
        if (this.ringsshown < this.rings) {
            this.ringsshown++;
            if (this.ringsshown == GFX_SONIC_DEAD_FLIP && this.highscores[this.ssely + 10] != 2) {
                this.spark = GFX_SONIC_CROUCHING;
                this.highscores[this.ssely + 10] = 2;
                SaveGame();
            }
        }
        MenuWait();
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        if (this.res != -1) {
            if (this.ringsshown < this.rings) {
                this.ringsshown = this.rings;
                if (this.ringsshown >= GFX_SONIC_DEAD_FLIP && this.highscores[this.ssely + 10] != 2) {
                    this.spark = GFX_SONIC_CROUCHING;
                    this.highscores[this.ssely + 10] = 2;
                    SaveGame();
                    return;
                }
                return;
            }
            if (demoStarted) {
            if (this.lselx == 0) {
            this.lselx = 1;
            } else if (this.lselx == 1) {
            this.lselx = 2;
            } else if (this.lselx == 2) {
            if (this.lsely != 6) {
    this.lselx = 0;
    this.lsely = this.lsely + 1;
    } else {
    this.LoopModeWanted = 2;
    return;
    }
}
                //this.lselx++;
                this.RestartPoint = 0;
                this.tutdone = 0;
                this.key_fire = 0;
                this.MenuTimer = 5;
                this.ssely = this.lselx + (this.lsely * 3);
                this.zone = this.ssely / 3;
                this.sect = this.ssely - (this.zone * 3);
                this.gzone = this.zone;
                this.act = this.sect;
                if (this.gzone > 5) {
                    this.gzone = 5;
                }
                if (this.zone == 6) {
                    this.act = 1;
                }
                this.LoopModeWanted = 9;
                return;
            } else {
            this.LoopModeWanted = 5;
            if (this.ssely == 2) {
                this.csnum = 14;
                currentIniCutscene = 1;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 5;
}
            }
            if (this.ssely == 5) {
                this.csnum = 20;
                currentIniCutscene = 2;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 5;
}
            }
            if (this.ssely == 8) {
                this.csnum = 22;
                currentIniCutscene = 3;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 5;
}
            }
            if (this.ssely == 11) {
                this.csnum = 32;
                currentIniCutscene = 4;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 5;
}
            }
            if (this.ssely == 14) {
                this.csnum = GFX_GREEN_BONUS_GO_A;
                currentIniCutscene = 5;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 5;
}
            }
            if (this.ssely == 17) {
                this.csnum = GFX_ZONE_2_ENEMY_B_FLIP;
                currentIniCutscene = 6;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else if (this.bonusunlocked > 0 && this.rings >= 50) {
this.LoopModeWanted = 5;
} else {
this.LoopModeWanted = 2;
}
                this.highscores[5] = 1;
            }
            if (this.ssely == 20) {
                this.csnum = GFX_SMOKE_5;
                currentIniCutscene = 8;
                if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else {
this.LoopModeWanted = 2;
}
            }
            this.ssely++;
            if (this.bonusunlocked == 0) {
                if (this.ssely == 18) {
                    this.ssely = 0;
                }
            } else {
                if (this.ssely == 21) {
                    this.ssely = 18;
                }
                if (this.ssely >= 18) {
                    this.bdun = 0;
                    this.n = 18;
                    while (this.n < 21) {
                        if (this.highscores[this.n + 10] == 2) {
                            this.bdun++;
                        }
                        this.n++;
                    }
                    if (this.bdun == 3) {
                        this.csnum = GFX_SHARD_2;
                        currentIniCutscene = 7;
                        if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else if (this.lselx != 2) {
this.LoopModeWanted = 9;
} else if (this.lsely == 6) {
this.LoopModeWanted = 2;
} else if (this.lsely != 6)  {
this.LoopModeWanted = 9;
}
                    }
                }
            }
            }
            if (!Paying.isPayed(0)) {
                byte pay = Paying.getStatus(0);
                if (pay < 5) {
                    Paying.setStatus(0, (byte) (pay + 1));
                }
                this.m_nPayPoint = 1;
                setPaying(0);
            }
        }
    }

    private void ProcessLangMenu() {
        /*if (this.multiLang.equals("lang.dat")) {
            language = 1;
            this.highscores[3] = (byte) language;
            SaveGame();
            this.LoopModeWanted = 15;
            if (this.splashdone == 0) {
                this.LoopModeWanted = 10;
                if (this.SoundAvailable == 0) {
                    if (!this.SkipRotation) {
                        this.LoopModeWanted = 29;
                        return;
                    } else {
                        this.LoopModeWanted = 10;
                        return;
                    }
                }
                return;
            }
            return;
        }*/
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            //this.menu[3] = 0;
            this.ilang = language;
            if (language > 0) {
                setsoftkeys(1, 2);
            } else {
                setsoftkeys(1, 0);
            }
            /*if (language > 0) {
                this.menu[3] = (byte) (language - 1);
                this.slidey = this.GameHeight;
            }*/
            this.slidey = this.GameHeight;
        }
        int n = 0;
        if (this.ilang > 0) {
            n = 1;
        }
        DoMenu(3, 6);
        if (n == 1 && this.res != -1) {
            this.musicwanted = 1;
        }
        if (this.res == 6) {
            this.LoopModeWanted = 15;
        }
        if (this.res >= 0 && this.res <= 8) {
            language = this.menu[3] + 1;
            this.highscores[3] = (byte) language;
            SaveGame();
            initLanguage(language);
            this.LoopModeWanted = 15;
            if (this.splashdone == 0) {
                this.LoopModeWanted = 17;
                if (this.SoundAvailable == 0) {
                    if (!this.SkipRotation) {
                        this.LoopModeWanted = 29;
                    } else {
                        this.LoopModeWanted = 10;
                    }
                }
            }
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 15;
        }
    }

    private void ProcessDelRecords() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            this.menu[21] = 1;
            setsoftkeys(1, 2);
            this.slidey = this.GameHeight;
        }
        DoMenu(21, 2);
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        if (this.res == 0) {
            this.ssely = 0;
            setlock(1);
            resetscores();
            this.ssely = 0;
            this.highscores[5] = 0;
            SaveGame();
            this.LoopModeWanted = 15;
        }
        if (this.res == 1) {
            this.LoopModeWanted = 15;
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 15;
        }
    }

    private void ProcessSoundMenu() {
        if (this.FirstFrame == 1) {
            this.DevPic = null;
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            this.menu[17] = 0;
            setsoftkeys(1, 0);
        }
        DoMenu(17, 3);
        if (this.res >= 0) {
            if (this.SoundAvailable == 3) {
                if (this.res == 0) {
                    this.SoundOnOff = 2;
                    Fever = false;
                    JumpPro = false;
                } else if (this.res == 1) {
                    //this.SoundOnOff = 0;
                    this.SoundOnOff = 2;
                    JumpPro = true;
                    Fever = false;
                } else if (this.res == 2) {
                    this.SoundOnOff = 2;
                    Fever = true;
                    JumpPro = false;
                }
            } else if (this.SoundAvailable == 2) {
                if (this.res == 0) {
                    this.SoundOnOff = 2;
                    Fever = false;
                    JumpPro = false;
                } else if (this.res == 1) {
                    //this.SoundOnOff = 0;
                    this.SoundOnOff = 2;
                    JumpPro = true;
                    Fever = false;
                } else if (this.res == 2) {
                    this.SoundOnOff = 2;
                    Fever = true;
                    JumpPro = false;
                }
            } else if (this.SoundAvailable == 1) {
                if (this.res == 0) {
                    this.SoundOnOff = 1;
                    Fever = false;
                    JumpPro = false;
                } else if (this.res == 1) {
                    //this.SoundOnOff = 0;
                    this.SoundOnOff = 1;
                    JumpPro = true;
                    Fever = false;
                } else if (this.res == 2) {
                    this.SoundOnOff = 1;
                    Fever = true;
                    JumpPro = false;
                }
            }
            this.highscores[0] = (byte) this.SoundOnOff;
            SaveGame();
            if (!soundtrack) {
            this.LoopModeWanted = 12;
            } else {
            this.LoopModeWanted = 15;
            soundtrack = false;
            }
        }
    }

    public void hideNotify() {
        super.hideNotify();
    }

    private void ProcessInterrupted() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 0);
            this.menu[7] = 0;
            this.olay = 1;
            this.musicwanted = -1;
        }
        DoMenu(7, 1);
        if (this.res == 0) {
            clearkeys();
            LoopMode = this.ResumeMode;
            this.LoopModeWanted = this.ResumeModeWanted;
            this.musicwanted = ResumeSound;
            setsoftkeys(this.ResumeSK1, this.ResumeSK2);
            PauseAdjust = false;
        }
    }

    private void ProcessExitConfirm() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.menu[8] = 1;
            this.slidex = this.GameWidth;
        }
        DoMenu(8, 2);
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        if (this.res == 0) {
            this.musicwanted = -1;
            this.LoopModeWanted = -2;
        }
        if (this.res == 1) {
            this.LoopModeWanted = 2;
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 2;
        }
    }

    private void ProcessLinkConfirm() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.menu[34] = 1;
            this.slidex = this.GameWidth;
        }
        DoMenu(34, 2);
        if (this.res != -1) {
            this.musicwanted = 1;
        }
        if (this.res == 0) {
            this.musicwanted = -1;
            Standard.menuMoreGameSelected();
            this.key_exit = 1;
        }
        if (this.res == 1) {
            this.LoopModeWanted = 2;
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 2;
        }
    }

    private void ProcessConfirmMain() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.menu[26] = 1;
            this.slidex = this.GameWidth;
        }
        DoMenu(26, 2);
        if (this.res != -1) {
            this.musicwanted = -1;
        }
        if (this.res == 0) {
            if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
                processEndDemo();
                this.LoopModeWanted = 32;
            } else {
                this.LoopModeWanted = 2;
            }
        }
        if (this.res == 1) {
            this.LoopModeWanted = 13;
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 13;
        }
    }

    private void ProcessPausescreen() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 6);
            this.menu[13] = 0;
            this.musicwanted = -1;
            processSound(2, 0, 0);
        }
        int n = 0;
        if (this.SoundAvailable == 1 || this.SoundAvailable == 2) {
            n = 1;
        }
        if (this.SoundAvailable == 3) {
            n = 2;
        }
        DoMenu(13, n + 4 + 1 + 1);
        if (this.res == 0) {
            this.LoopModeWanted = 1;
        }
        if (this.res == 1) {
            if (this.lives <= 2) {
                initgame();
            } else {
                this.RestartPoint = 0;
            }
            this.LoopModeWanted = 9;
        }
        if (n == 1) {
            if (this.res == 2) {
                if (this.SoundAvailable == 1) {
                    incsound();
                } else {
                    incmusic();
                }
                SaveGame();
                clearkeys();
                if (this.SoundAvailable == 1) {
                    this.musicwanted = -1;
                    processSound(1, -1, 1);
                }
            }
        } else if (n == 2) {
            if (this.res == 2) {
                incsound();
                SaveGame();
                clearkeys();
                this.musicwanted = -1;
                processSound(1, -1, 1);
            }
            if (this.res == 3) {
                incmusic();
                SaveGame();
                clearkeys();
            }
        }
        if (this.res == n + 2) {
            this.VibraOnOff = 1 - this.VibraOnOff;
            this.vibrawanted = 2;
            this.highscores[1] = (byte) this.VibraOnOff;
            SaveGame();
            clearkeys();
        }
        if (this.res == n + 2 + 1) {
            this.TouchpadVisibleOnOff = 1 - this.TouchpadVisibleOnOff;
        }
        if (this.res == n + 2 + 1 + 1) {
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            this.textheading = 5;
            FontAdjust = false;
            helpAdjust = true;
            inittext(localText[GFX_ZONE_3_ENEMY_B]);
            this.LoopModeWanted = 16;
        }
        if (this.res == n + 3 + 1 + 1) {
            this.LoopModeWanted = 26;
        }
    }

    private void ProcessInfo() {
        if (this.FirstFrame == 1) {
            this.musicwanted = 1;
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(0, 2);
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            if (this.slideyoff == 0 && this.slideyoff == 0) {
                if (this.key_back == 1) {
                    FontAdjust = true;
                    helpAdjust = false;
                    this.key_back = 0;
                    this.LoopModeWanted = this.modefrom;
                    return;
                } else if (this.key_up > 0) {
                    if (this.lineshown > 0) {
                        this.lineshown--;
                    }
                    this.MenuTimer = 1;
                    return;
                } else {
                    if (this.key_down > 0) {
                        if (this.lineshown + this.maxlines <= this.numstrings) {
                            this.lineshown++;
                        }
                        this.MenuTimer = 1;
                        return;
                    }
                    return;
                }
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessUpsell() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            if (this.upsell.equals("2")) {
                setsoftkeys(1, 2);
            } else if (this.upsell.equals("1")) {
                setsoftkeys(0, 2);
            }
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            if (this.key_fire == 1 && this.upsell.equals("2")) {
                this.key_fire = 0;
                this.LoopModeWanted = 28;
            }
            if (this.key_back == 1) {
                this.key_back = 0;
                this.LoopModeWanted = 2;
            }
            if (this.key_up > 0) {
                if (this.lineshown > 0) {
                    this.lineshown--;
                }
                this.MenuTimer = 5;
            }
            if (this.key_down > 0) {
                if (this.lineshown + this.maxlines <= this.numstrings) {
                    this.lineshown++;
                }
                this.MenuTimer = 5;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessLaunch() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.menu[28] = 0;
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            DoMenu(28, 2);
            if (this.res == 0) {
                launchBrowser(this.upsellURL);
                browser = true;
                browsers = 1;
            }
            if (this.res == 1) {
                launchBrowser(this.OnlineLink);
                browser = true;
                browsers = 2;
            }
            if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessRotation() {
        setsoftkeys(0, 0);
        rotationland = rotationland ? false : true;
        //this.m_Canvas
        int temp = this.GameWidth;
        this.GameWidth = this.GameHeight;
        this.GameHeight = temp;
        int w = 0;
        int h = 0;
        if (rotationland) {
        w = (getWidth() * 16) / 9;
        h = getWidth();
        Shell.sInstance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
        w = this.WIDTH;
        h = this.HEIGHT;
        Shell.sInstance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        this.scale_x = ((float) this.GameWidth) / ((float) w);
        this.scale_y = ((float) this.GameWidth) / ((float) h);
        this.src = new Rect(0, 0, this.GameWidth, this.GameHeight);
        this.dst_m = new Rect(0, 0, w, h);
        this.dst = new Rect(0, 0, w, h);
        this.G = new Graphics(this.GameWidth, this.GameHeight, 0, 0);
        this.G.wg_setFontSize(24);

        if (!this.rotating) {
            this.brandImage = null;
            this.LoopModeWanted = 10;
        } else if (System.currentTimeMillis() - this.displayTime > 2000) {
            this.displayTime = System.currentTimeMillis();
            try {
                this.brandImage.LoadPic("brand" + this.nImageNumber);
                this.nImageNumber++;
            } catch (Exception e) {
                this.rotating = false;
                this.LoopModeWanted = 10;
            }
        }
    }

    private void ProcessDemoStart() {
        this.demoStarted = false;
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(8, 7);
            if (vodaDemo == null || vodaDemo == "") {
                this.slidex = this.GameWidth;
            }
        }
            if (!DemoFurikan) {
            demoSecs = 2040;
            } else {
            demoSecs = 60 * 5;
            }
            demoLastSecs = 0;
        if (vodaDemo.equals("1")) {
            processSound(2, 0, 0);
            this.musicwanted = -1;
        }
        //if (this.key_fire == 1) {
            this.key_fire = 0;
            startDemo();
            this.lselx = 0;
            if (!DemoFurikan) {
            this.lsely = 0;
            } else {
            this.lsely = 0;
            }
            this.ssely = this.lselx + (this.lsely * 3);
            this.zone = this.ssely / 3;
            this.sect = this.ssely - (this.zone * 3);
            this.gzone = this.zone;
            this.act = this.sect;
            this.RestartPoint = 0;
            //initgame();
            if (!DemoFurikan) {
            this.lives = -1;
            } else {
            this.lives = 2;
            }
            this.LoopModeWanted = 9;
            this.tut2done = 0;
            this.tutdone = 0;
        //}
        /*if (this.key_back == 1) {
            this.key_exit = 1;
        }*/
    }

    private void ProcessDemoEnd() {
        if (vodaDemo.equals("1")) {
            if (this.FirstFrame == 1) {
                this.MenuTimer = 10;
                this.FirstFrame = 0;
                if (demoURL != null && this.demoMode.equals("2")) {
                    setsoftkeys(9, 10);
                } else {
                    setsoftkeys(0, 10);
                }
                processSound(2, 0, 0);
                this.musicwanted = -1;
            }
            if (this.key_fire == 1) {
                this.key_fire = 0;
                if (demoURL != "" && demoURL != null && this.demoMode.equals("2")) {
                    launchBrowser(demoURL);
                }
            }
            if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        if (this.FirstFrame == 1) {
            this.musicwanted = 1;
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            if (demoURL == "" || demoURL == null || this.demoMode.equals("1")) {
                setsoftkeys(0, 10);
            } else {
                setsoftkeys(9, 10);
            }
            this.slidex = this.GameWidth;
        }
        if (this.MenuTimer == 0) {
            if (this.key_fire == 1) {
                this.key_fire = 0;
                if ((demoURL != "" || demoURL != null) && this.demoMode.equals("2")) {
                    launchBrowser(demoURL);
                }
            }
            if (this.key_back == 1) {
                this.key_back = 0;
                this.LoopModeWanted = 2;
            }
            if (this.key_up > 0) {
                if (this.lineshown > 0) {
                    this.lineshown--;
                }
                this.MenuTimer = 5;
            }
            if (this.key_down > 0) {
                if (this.lineshown + this.maxlines <= this.numstrings) {
                    this.lineshown++;
                }
                this.MenuTimer = 5;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessNokiaSku() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 0);
        }
        if (this.MenuTimer == 0) {
            if (this.key_fire == 1) {
                this.key_fire = 0;
                if (language != 0) {
                    initLanguage(language);
                    if (!this.SkipRotation) {
                        this.LoopModeWanted = 29;
                    } else {
                        this.LoopModeWanted = 10;
                    }
                } else {
                    if (this.multiLang.equals("1")) {
                        this.LoopModeWanted = 3;
                    } else {
                        this.LoopModeWanted = 3;
                    }
                    LoopMode = this.LoopModeWanted;
                    this.FirstFrame = 1;
                    this.olay = 1;
                    this.MenuTimer = 10;
                    Cls(0);
                }
            }
            if (this.key_up > 0) {
                if (this.lineshown > 0) {
                    this.lineshown--;
                }
                this.MenuTimer = 5;
            }
            if (this.key_down > 0) {
                if (this.lineshown + this.maxlines <= this.numstrings) {
                    this.lineshown++;
                }
                this.MenuTimer = 5;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessZoneSelect() {
        if (this.FirstFrame == 1) {
            BigFont = false;
            FontAdjust = true;
            RegularFont = false;
            FontRightAdjust = false;
            this.musicwanted = 1;
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 2);
            this.RestartPoint = 0;
            this.lselx = this.ssely % 3;
            this.lsely = this.ssely / 3;
            int oldbonusunlocked = this.bonusunlocked;
            this.bonusunlocked = 1;
            this.n = 0;
            while (this.n < 18) {
                if (this.highscores[this.n + 10] != 2) {
                    this.bonusunlocked = 0;
                }
                this.n++;
            }
            if (this.bonusunlocked == 1 && oldbonusunlocked == 0) {
                this.ssely = 18;
            }
        }
        if (this.bonusunlocked == 1) {
            this.totalZonesUnlocked = 6;
        } else {
            this.totalZonesUnlocked = 5;
        }
        if (this.MenuTimer == 0) {
            if (this.key_up == 1) {
                if (this.lsely > 0) {
                    this.lsely--;
                } else if (this.lselx == 2) {
                    int k = this.totalZonesUnlocked;
                    while (k >= 0) {
                        if (this.highscores[(k * 3) + 10 + 2] != 0) {
                            this.lsely = k;
                            k = 0;
                        }
                        k--;
                    }
                } else if (this.lselx == 1) {
                    int k2 = this.totalZonesUnlocked;
                    while (k2 >= 0) {
                        if (this.highscores[(k2 * 3) + 10 + 1] != 0) {
                            this.lsely = k2;
                            k2 = 0;
                        }
                        k2--;
                    }
                } else if (this.lselx == 0) {
                    int k3 = this.totalZonesUnlocked;
                    while (k3 >= 0) {
                        if (this.highscores[(k3 * 3) + 10 + 0] != 0) {
                            this.lsely = k3;
                            k3 = 0;
                        }
                        k3--;
                    }
                }
                if (this.lastpressed == 0) {
                    this.lastpressed = 1;
                    this.MenuTimer = 5;
                }
            }
            if (this.key_down == 1) {
                if (this.highscores[(this.lsely * 3) + 10 + this.lselx + 3] != 0) {
                    this.lsely++;
                } else if (this.lselx == 1) {
                    if (this.highscores[(this.lsely * 3) + 10 + 0 + 3] != 0) {
                        this.lsely++;
                        this.lselx = 0;
                    } else {
                        this.lsely = 0;
                    }
                } else if (this.lselx == 2) {
                    if (this.highscores[(this.lsely * 3) + 10 + 1 + 3] != 0) {
                        this.lsely++;
                        this.lselx = 1;
                    } else if (this.highscores[(this.lsely * 3) + 10 + 0 + 3] != 0) {
                        this.lsely++;
                        this.lselx = 0;
                    } else {
                        this.lsely = 0;
                    }
                } else {
                    this.lsely = 0;
                }
                if (this.lsely > this.totalZonesUnlocked) {
                    this.lsely = 0;
                }
                if (this.lastpressed == 0) {
                    this.lastpressed = 1;
                    this.MenuTimer = 5;
                }
            }
            if (this.key_left == 1) {
                if (this.lselx > 0) {
                    this.lselx--;
                } else if (this.lsely > 0) {
                    this.lsely--;
                    this.lselx = 2;
                } else {
                    int k4 = this.totalZonesUnlocked;
                    while (k4 >= 0) {
                        if (this.highscores[(k4 * 3) + 10 + 2] != 0) {
                            this.lsely = k4;
                            this.lselx = 2;
                            k4 = 0;
                        } else if (this.highscores[(k4 * 3) + 10 + 1] != 0) {
                            this.lsely = k4;
                            this.lselx = 1;
                            k4 = 0;
                        } else if (this.highscores[(k4 * 3) + 10 + 0] != 0) {
                            this.lsely = k4;
                            this.lselx = 0;
                            k4 = 0;
                        }
                        k4--;
                    }
                }
                if (this.lastpressed == 0) {
                    this.lastpressed = 1;
                    this.MenuTimer = 5;
                }
            }
            if (this.key_right == 1) {
                if (this.lselx < 2) {
                    if (this.highscores[(this.lsely * 3) + 10 + this.lselx + 1] != 0) {
                        this.lselx++;
                    } else {
                        this.lsely = 0;
                        this.lselx = 0;
                    }
                } else if (this.highscores[(this.lsely * 3) + 10 + this.lselx + 1] != 0) {
                    this.lsely++;
                    if (this.lsely > this.totalZonesUnlocked) {
                        this.lsely = 0;
                    }
                    this.lselx = 0;
                } else {
                    this.lsely = 0;
                    this.lselx = 0;
                }
                if (this.lastpressed == 0) {
                    this.lastpressed = 1;
                    this.MenuTimer = 5;
                }
            }
            if (this.key_fire == 1) {
                this.tutdone = 0;
                this.key_fire = 0;
                this.MenuTimer = 5;
                this.ssely = this.lselx + (this.lsely * 3);
                this.zone = this.ssely / 3;
                this.sect = this.ssely - (this.zone * 3);
                this.gzone = this.zone;
                this.act = this.sect;
                if (this.gzone > 5) {
                    this.gzone = 5;
                }
                if (this.zone == 6) {
                    this.act = 1;
                }
                this.LoopModeWanted = 9;
                
                if (OnlineIn != null) this.lives = life[this.ssely];
                if (this.ssely == 0) {
                    this.csnum = 1;
                    currentIniCutscene = 0;
                    if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else if (this.lselx != 2) {
this.LoopModeWanted = 9;
} else if (this.lsely == 6) {
this.LoopModeWanted = 2;
} else if (this.lsely != 6)  {
this.LoopModeWanted = 9;
}
                }
                if (this.ssely == 18) {
                    this.csnum = GFX_SMOKE_1;
                    currentIniCutscene = 7;
                    if (sonic) {
this.LoopModeWanted = 22;
} else if (shadow && shadowstory) {
this.LoopModeWanted = 40;
} else if (this.lselx != 2) {
this.LoopModeWanted = 9;
} else if (this.lsely == 6) {
this.LoopModeWanted = 2;
} else if (this.lsely != 6)  {
this.LoopModeWanted = 9;
}
                }
            }
            if (this.key_back == 1) {
                this.LoopModeWanted = 2;
                return;
            }
            return;
        }
        this.MenuTimer--;
    }

    private void ProcessReady() {
        if (this.FirstFrame == 1) {    
            this.MenuTimer = 10;
            this.FirstFrame++;
            this.menu[9] = 0;
            setsoftkeys(1, 0);
            this.currentmap = this.ssely;
            this.zone = this.ssely / 3;
            this.sect = this.ssely - (this.zone * 3);
            this.act = this.sect;
            if (this.zone == 6) {
                this.act = 1;
            }
            this.gzone = this.zone;
            if (this.gzone > 5) {
                this.gzone = 5;
            }
            InitRoom();
            this.olay = 1;
            this.bossmode = -1;
        }
        if (this.FirstFrame < 6 && this.FirstFrame != 0) {
            this.FirstFrame++;
        }
        if (!this.StartSoundPlayed && this.FirstFrame == 5) {
            this.musicwanted = 100;
            this.StartSoundPlayed = true;
            this.FirstFrame = 0;
        }
        DoMenu(9, 1);
        if (this.res == 0) {
            this.MenuTimer = 5;
            this.LoopModeWanted = 1;
        }
        if (this.key_back == 1) {
            this.LoopModeWanted = 5;
        }
    }

    void settut(int txt) {
        if (this.tutdone == 0) {
            this.tut = 1;
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            RegularFont = false;
            BigFont = false;
            helpAdjust = false;
            FontAdjust = true;
            tutAdjust = true;
            inittext(localText[txt]);
        }
    }

    private void ProcessLevelFailed() {
        if (this.FirstFrame == 1) {
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 0);
            this.menu[6] = 0;
            this.olay = 1;
            this.livesshown = this.lives;
        }
        if (this.livesshown == 0) {
            DoMenu(6, 2);
            if (this.res == 0) {
                initgame();
                this.LoopModeWanted = 9;
            }
            if (this.res == 1) {
                if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
                    processEndDemo();
                    this.LoopModeWanted = 32;
                    return;
                } else {
                    this.LoopModeWanted = 2;
                    return;
                }
            }
            return;
        }
        MenuWait();
        if (this.res != -1) {
        if (!demoStarted) {
            this.lives--;
            }
            this.LoopModeWanted = 9;
        }
    }

    private void ProcessLevelDone() {
        if (this.FirstFrame == 1) {
            int dosave = 0;
            this.MenuTimer = 10;
            this.FirstFrame = 0;
            setsoftkeys(1, 0);
            this.menu[14] = 0;
            this.olay = 1;
            if (this.ssely < 20 && ((this.ssely != 17 || this.rings >= GFX_SONIC_DEAD_FLIP) && this.highscores[this.ssely + 10 + 1] == 0)) {
                this.highscores[this.ssely + 10 + 1] = 1;
                dosave = 1;
            }
            this.ringsshown = 0;
            this.timebonus = 12500;
            if (this.mins > 1) {
                this.timebonus -= (((this.mins - 1) * GFX_ZONE_1_ENEMY_B) + this.secs) * 100;
                if (this.timebonus < 0) {
                    this.timebonus = 0;
                }
            }
            int tot = this.score + (this.rings * 100) + this.timebonus;
            int s0 = getscore(this.ssely, 0);
            if (s0 < 0) {
                s0 = 0 - s0;
            }
            this.gothiscore = 1;
            if (tot > s0) {
                setscore(this.ssely, 2, getscore(this.ssely, 1));
                setscore(this.ssely, 1, getscore(this.ssely, 0));
                setscore(this.ssely, 0, tot);
                dosave = 1;
                this.lasthiscore = (this.zone * 9) + (this.sect * 3) + 0;
            } else {
                int s1 = getscore(this.ssely, 1);
                if (s1 < 0) {
                    s1 = 0 - s1;
                }
                if (tot > s1) {
                    setscore(this.ssely, 2, getscore(this.ssely, 1));
                    setscore(this.ssely, 1, tot);
                    dosave = 1;
                    this.lasthiscore = (this.zone * 9) + (this.sect * 3) + 1;
                } else {
                    int s2 = getscore(this.ssely, 2);
                    if (s2 < 0) {
                        s2 = 0 - s2;
                    }
                    if (tot > s2) {
                        setscore(this.ssely, 2, tot);
                        dosave = 1;
                        this.lasthiscore = (this.zone * 9) + (this.sect * 3) + 2;
                    } else {
                        this.gothiscore = 0;
                    }
                }
            }
            if (dosave != 0) {
                SaveGame();
            }
        }
        DoMenu(14, 2);
        if (this.lselx == 2 && this.lsely == 5 && speedtime < 2040 - demoSecs) {
        speedtime = 2040 - demoSecs;
        }
        if (this.lselx == 2 && this.lsely == 6 && speedtime2 < 2040 - demoSecs) {
        speedtime2 = 2040 - demoSecs;
        }
        if (this.res == 0) {
            this.LoopModeWanted = 24;
        }
        if (this.res == 1) {
            if (this.demoMode.equals("1") || this.demoMode.equals("2")) {
                this.lselx++;
                this.RestartPoint = 0;
                this.tutdone = 0;
                this.key_fire = 0;
                this.MenuTimer = 5;
                this.ssely = this.lselx + (this.lsely * 3);
                this.zone = this.ssely / 3;
                this.sect = this.ssely - (this.zone * 3);
                this.gzone = this.zone;
                this.act = this.sect;
                if (this.gzone > 5) {
                    this.gzone = 5;
                }
                if (this.zone == 6) {
                    this.act = 1;
                }
                this.LoopModeWanted = 9;
                return;
            }
            this.LoopModeWanted = 5;
        }
    }

    void setlevelfailed() {
        if (this.levelover == 0) {
            this.levelover = 2;
            this.levelovertime = GFX_ANTENNA_OFF;
            this.musicwanted = 102;
        }
    }

    void setlevelwon() {
        if (this.levelover == 0) {
            this.levelover = 1;
            this.levelovertime = 30;
            this.musicwanted = 101;
        }
    }

    void rotatechain(int rot, int rad) {
        int rot2 = rot & 255;
        int a1 = rot2 >> 4;
        int a2 = (a1 + 1) & 15;
        int pc = rot2 & 15;
        int dx1 = (this.sintab[a1] * rad) >> 8;
        int dy1 = (this.sintab[a1 + 4] * rad) >> 8;
        int dx2 = (this.sintab[a2] * rad) >> 8;
        int dy2 = (this.sintab[a2 + 4] * rad) >> 8;
        int dx = ((dx2 - dx1) * pc) >> 4;
        int dy = ((dy2 - dy1) * pc) >> 4;
        this.chain1x = dx + dx1;
        this.chain1y = -(dy + dy1);
    }

    void initjump() {
        this.firstjump = 0;
        this.superjump = 0;
        this.inertia = -9;
        this.hurt = 0;
        this.sonicmode = 3;
    }

    private void processPlatform(int n) {
        if (this.tut == 0) {
            if (this.enemydata[n] > 0) {
                int[] iArr = this.enemydata;
                iArr[n] = iArr[n] - 2;
            }
            if (this.enemyy[n] >= this.scrolly + 128) {
                this.enemytype[n] = -1;
            }
        }
    }

    private void processPlatformCrumbly(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] + 1;
        }
        if (this.enemydata[n] == 10) {
            this.enemytype[n] = 16;
            this.enemydata[n] = GFX_SONIC_DEAD_FLIP;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformSpikey(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] - 2;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processRing(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] + 1;
        }
        if (this.enemydata[n] == 6) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformSpring(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] - 2;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processGreenBonus(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] + 1;
            if (this.enemydata[n] == 12) {
                this.enemytype[n] = -1;
            }
        }
    }

    private void processBlueBonus(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] + 1;
            if (this.enemydata[n] == GFX_ALTIMETER_POINTER) {
                this.enemydata[n] = 0;
            }
        }
    }

    private void processFlyingCreature(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] - 1;
            if (this.enemydata[n] == 0) {
                byte[] bArr = this.enemyx;
                bArr[n] = (byte) (bArr[n] + (this.enemymove[n] - 9));
                this.sfxwanted[3] = 1;
                this.vibrawanted = 2;
                this.enemytype[n] = 19;
                this.enemydata[n] = 0;
                this.score += 2000;
                return;
            }
            return;
        }
        if (this.enemydir[n] == 0) {
            int[] iArr2 = this.enemymove;
            iArr2[n] = iArr2[n] + 1;
            if (this.enemymove[n] == GFX_EXPLOSION_D) {
                this.enemydir[n] = 1;
                return;
            }
            return;
        }
        int[] iArr3 = this.enemymove;
        iArr3[n] = iArr3[n] - 1;
        if (this.enemymove[n] == 0) {
            this.enemydir[n] = 0;
        }
    }

    private void processCheckpoint(int n) {
        if (this.sect < 2) {
            if (this.levelover == 1) {
                this.srot += 32;
                if (this.srot == 256) {
                    this.spic = 1;
                }
                if (this.srot > 384) {
                    this.srot = 384;
                    return;
                }
                return;
            }
            return;
        }
        if (this.enemydata[n] > 0 && this.enemymove[n] < 16) {
            int[] iArr = this.enemymove;
            iArr[n] = iArr[n] + 2;
        }
        if (this.enemyy[n] >= this.scrolly + GFX_CRUSHER) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformSliding(int n) {
        if (this.enemydata[n] > 0) {
            int[] iArr = this.enemydata;
            iArr[n] = iArr[n] - 2;
        }
        if (this.enemydir[n] == 0) {
            int[] iArr2 = this.enemymove;
            iArr2[n] = iArr2[n] + 1;
            if (this.enemymove[n] == GFX_EXPLOSION_D) {
                this.enemydir[n] = 1;
            }
        } else {
            int[] iArr3 = this.enemymove;
            iArr3[n] = iArr3[n] - 1;
            if (this.enemymove[n] == 0) {
                this.enemydir[n] = 0;
            }
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processTractor(int n) {
        int[] iArr = this.enemyy;
        iArr[n] = iArr[n] - 2;
        int[] iArr2 = this.enemydata;
        iArr2[n] = iArr2[n] + 1;
        if (this.enemydata[n] == 6) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformCrumblySliding(int n) {
        if (this.enemydir[n] == 0) {
            int[] iArr = this.enemymove;
            iArr[n] = iArr[n] + 1;
            if (this.enemymove[n] == GFX_EXPLOSION_D) {
                this.enemydir[n] = 1;
            }
        } else {
            int[] iArr2 = this.enemymove;
            iArr2[n] = iArr2[n] - 1;
            if (this.enemymove[n] == 0) {
                this.enemydir[n] = 0;
            }
        }
        if (this.enemydata[n] > 0) {
            int[] iArr3 = this.enemydata;
            iArr3[n] = iArr3[n] + 1;
        }
        if (this.enemydata[n] == 10) {
            this.enemytype[n] = 17;
            this.enemydata[n] = GFX_SONIC_DEAD_FLIP;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processAntenna(int n) {
        if (this.enemydata[n] > 0 && this.enemymove[n] < 16) {
            int[] iArr = this.enemymove;
            iArr[n] = iArr[n] + 2;
        }
        if (this.enemyy[n] >= this.scrolly + GFX_CRUSHER) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformCrumblyWaiting(int n) {
        int[] iArr = this.enemydata;
        iArr[n] = iArr[n] - 1;
        if (this.bossmode == 2) {
            this.enemydata[n] = 0;
        }
        if (this.enemydata[n] == 0) {
            this.enemytype[n] = 1;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processPlatformCrumblySlidingWaiting(int n) {
        int[] iArr = this.enemydata;
        iArr[n] = iArr[n] - 1;
        if (this.bossmode == 2) {
            this.enemydata[n] = 0;
        }
        if (this.enemydata[n] == 0) {
            this.enemytype[n] = 13;
        }
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processDebrisRing(int n) {
        byte[] bArr = this.enemyx;
        bArr[n] = (byte) (bArr[n] + this.enemymove[n]);
        int[] iArr = this.enemyy;
        iArr[n] = iArr[n] + this.enemydata[n];
        int[] iArr2 = this.enemydata;
        iArr2[n] = iArr2[n] + 1;
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void processExplosion(int n) {
        int[] iArr = this.enemydata;
        iArr[n] = iArr[n] + 1;
        if (this.enemydata[n] == 8) {
            this.enemytype[n] = -1;
        }
    }

    private void processBoss(int n) {
        if (this.tut == 0) {
            if (this.bossbob > 0) {
                this.bossbob--;
            }
            if (this.bosshurt > 0) {
                this.bosshurt--;
            }
            switch (this.bossmode) {
                case 0:
                    this.bosslistptr = this.bossstart[this.zone];
                    this.bosstime = this.bosslist[(this.bosslistptr * 5) + 2];
                    this.bossmode = 1;
                    this.chain1x = 0;
                    this.chain1y = 5;
                    this.chain2x = 0;
                    this.chain2y = 5;
                    this.chainrot1 = GFX_BALL3;
                    this.chainvel = 11;
                    this.chainlen = 24;
                    this.chainfall = 0;
                    this.chainfallg = 0;
                    break;
                case 1:
                    this.b = this.bosslistptr * 5;
                    this.dx = this.bosslist[this.b + 0];
                    this.dy = this.bosslist[this.b + 1];
                    this.bosstime--;
                    this.perc = (this.bosstime * 20) / this.bosslist[this.b + 2];
                    if (this.perc < 3 || this.perc > 16) {
                        this.dx >>= 1;
                        this.dy >>= 1;
                    }
                    if (this.perc > 6 && this.perc < 13) {
                        this.dx += this.dx >> 1;
                        this.dy += this.dy >> 1;
                    }
                    int[] iArr = this.enemyy;
                    iArr[n] = iArr[n] + this.dy;
                    this.realbossx += this.dx;
                    this.realbossy += this.dy;
                    this.enemyx[n] = (byte) (this.realbossx >> 1);
                    this.enemyy[n] = this.realbossy >> 1;
                    if (this.bosstime == 0) {
                        this.bosslistptr = this.bosslist[this.b + 3];
                        this.bosstime = this.bosslist[(this.bosslistptr * 5) + 2];
                    }
                    this.b = this.bosslist[(this.bosslistptr * 5) + 4];
                    if (this.b == 1) {
                        if (this.bosstime < GFX_ANTENNA_OFF) {
                            this.chain1y = ((this.bosstime * GFX_ZONE_2_ENEMY_B) / GFX_ANTENNA_OFF) + 5;
                        } else {
                            this.chain1y = (((GFX_SONIC_DEAD_FLIP - this.bosstime) * GFX_ZONE_2_ENEMY_B) / 10) + 5;
                        }
                    }
                    if (this.b == 2) {
                        if (this.chainrot1 > 128) {
                            this.chainvel--;
                        }
                        if (this.chainrot1 < 128) {
                            this.chainvel++;
                        }
                        this.chainrot1 += this.chainvel;
                        rotatechain(this.chainrot1, 30);
                    }
                    if (this.b == 3) {
                        if (this.chainlen > 24) {
                            this.chainlen--;
                        }
                        this.chainrot1 += 4;
                        rotatechain(this.chainrot1, this.chainlen);
                    }
                    if (this.b == 4) {
                        if (this.chainlen < GFX_ZONE_1_ENEMY_B) {
                            this.chainlen++;
                        }
                        this.chainrot1 += 4;
                        rotatechain(this.chainrot1, this.chainlen);
                    }
                    if (this.b == 5) {
                        if (this.bosstime > GFX_ANTENNA_OFF) {
                            this.chain1y = (((GFX_SONIC_DEAD_FLIP - this.bosstime) * GFX_ZONE_2_ENEMY_B) / 10) + 5;
                        } else if (this.bosstime > 20) {
                            this.chain1y = (((this.bosstime - 20) * GFX_ZONE_2_ENEMY_B) / 20) + 5;
                        }
                        if (this.bosstime > 20) {
                            if (this.bosstime < 30) {
                                this.chain2y = (((30 - this.bosstime) * GFX_ZONE_2_ENEMY_B) / 10) + 5;
                            }
                        } else {
                            this.chain2y = ((this.bosstime * GFX_ZONE_2_ENEMY_B) / 20) + 5;
                        }
                    }
                    if (this.b == 6) {
                        this.chainrot1 += 4;
                        rotatechain(this.chainrot1, GFX_ANTENNA_OFF);
                        this.chain2x = this.chain1x;
                        this.chain2y = this.chain1y;
                        rotatechain(this.chainrot1 + 128, GFX_ANTENNA_OFF);
                    }
                    if (this.b == 7) {
                        this.chainrot1 += 2;
                        if (this.zone == 6) {
                            this.chainrot1++;
                        }
                        rotatechain(-this.chainrot1, 30);
                        this.chain2x = this.chain1x;
                        this.chain2y = this.chain1y;
                        rotatechain(this.chainrot1 << 1, GFX_ZONE_1_ENEMY_B);
                        break;
                    }
                    break;
                case 2:
                    this.bosstime--;
                    if (this.bosstime == 0) {
                        this.bossmode = 3;
                    }
                    this.chainfallg++;
                    this.chainfall += this.chainfallg;
                    break;
                case 3:
                    this.bossbob = 0;
                    int[] iArr2 = this.enemyy;
                    iArr2[n] = iArr2[n] - 1;
                    this.chainfallg++;
                    this.chainfall += this.chainfallg;
                    if (this.enemyy[n] <= -32) {
                        setlevelwon();
                        this.bossmode = -1;
                        break;
                    }
                    break;
            }
            if (this.bosshealth == 0 && (this.random.nextInt() & 255) > 200) {
                this.vibrawanted = 1;
                addsprite((this.enemyx[n] + (this.random.nextInt() % 16)) - 9, this.enemyy[n] + (this.random.nextInt() % 8), 19, 0);
            }
            if ((this.random.nextInt() & 255) > this.bosshealth) {
                this.h = 256;
                while (this.h > this.bosshealth) {
                    addsprite(this.enemyx[n] + 8 + (this.random.nextInt() % 4), this.enemyy[n] + (this.random.nextInt() % 4), 21, 0);
                    this.h -= 32;
                }
            }
        }
    }

    private void processSmoke(int n) {
        byte[] bArr = this.enemyx;
        bArr[n] = (byte) (bArr[n] + 2);
        int[] iArr = this.enemyy;
        iArr[n] = iArr[n] - 2;
        int[] iArr2 = this.enemydata;
        iArr2[n] = iArr2[n] + 1;
        if (this.enemydata[n] == 12) {
            this.enemytype[n] = -1;
        }
    }

    private void processDebrisShard(int n) {
        byte[] bArr = this.enemyx;
        bArr[n] = (byte) (bArr[n] + this.enemymove[n]);
        int[] iArr = this.enemyy;
        iArr[n] = iArr[n] + this.enemydata[n];
        int[] iArr2 = this.enemydata;
        iArr2[n] = iArr2[n] + 1;
        if (this.enemyy[n] >= this.scrolly + 128) {
            this.enemytype[n] = -1;
        }
    }

    private void ProcessGame() {
        if (this.FirstFrame == 1) {
            this.FirstFrame = 0;
            this.setsoftkeys(0, 4);
            this.MenuTimer = 5;
            this.musicwanted = this.gzone + 103;
        }

        if (this.invulnerable > 0) {
            --this.invulnerable;
        }

        int var2;
        label513:
        for(this.n = 0; this.n < this.si; var2 = this.n++) {
            this.type = this.enemytype[this.n];
            if (this.type > -1) {
                this.mby = this.enemyy[this.n] - this.scrolly;
                int var10002;
                byte[] var6;
                int[] var7;
                switch (this.type) {
                    case 0:
                        if ((this.mby <= 128 || this.mby >= -16) && this.tut == 0) {
                            if (this.enemydata[this.n] > 0) {
                                var7 = this.enemydata;
                                var2 = this.n;
                                var7[var2] -= 2;
                            }

                            if (this.enemyy[this.n] >= this.scrolly + 128) {
                                this.enemytype[this.n] = -1;
                            }
                        }
                        break;
                    case 1:
                        if (this.mby > 128 && this.mby < -16) {
                            break;
                        }

                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                        }

                        if (this.enemydata[this.n] == 10) {
                            this.enemytype[this.n] = 16;
                            this.enemydata[this.n] = 50;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 2:
                        if (this.mby > 128 && this.mby < -16) {
                            break;
                        }

                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var7[var2] -= 2;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 3:
                        if (this.mby > 128 && this.mby < -16) {
                            break;
                        }

                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                        }

                        if (this.enemydata[this.n] == 6) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 4:
                        if (this.mby > 128 && this.mby < -16) {
                            break;
                        }

                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var7[var2] -= 2;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 5:
                        if ((this.mby <= 128 || this.mby >= -16) && this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                            if (this.enemydata[this.n] == 12) {
                                this.enemytype[this.n] = -1;
                            }
                        }
                        break;
                    case 6:
                        if ((this.mby <= 128 || this.mby >= -16) && this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                            if (this.enemydata[this.n] == 36) {
                                this.enemydata[this.n] = 0;
                            }
                        }
                        break;
                    case 7:
                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]--;
                            if (this.enemydata[this.n] == 0) {
                                var6 = this.enemyx;
                                var2 = this.n;
                                var6[var2] = (byte)(var6[var2] + (this.enemymove[this.n] - 9));
                                this.sfxwanted[3] = 1;
                                this.vibrawanted = 2;
                                this.enemytype[this.n] = 19;
                                this.enemydata[this.n] = 0;
                                this.score += 2000;
                            }
                        } else if (this.enemydir[this.n] == 0) {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                            if (this.enemymove[this.n] == 48) {
                                this.enemydir[this.n] = 1;
                            }
                        } else {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]--;
                            if (this.enemymove[this.n] == 0) {
                                this.enemydir[this.n] = 0;
                            }
                        }
                        break;
                    case 8:
                        if (this.mby <= 128 || this.mby >= -16) {
                            if (this.sect < 2) {
                                if (this.levelover == 1) {
                                    this.srot += 32;
                                    if (this.srot == 256) {
                                        this.spic = 1;
                                    }

                                    if (this.srot > 384) {
                                        this.srot = 384;
                                    }
                                }
                            } else {
                                if (this.enemydata[this.n] > 0 && this.enemymove[this.n] < 16) {
                                    var7 = this.enemymove;
                                    var2 = this.n;
                                    var7[var2] += 2;
                                }

                                if (this.enemyy[this.n] >= this.scrolly + 120) {
                                    this.enemytype[this.n] = -1;
                                }
                            }
                        }
                        break;
                    case 9:
                    case 11:
                    case 12:
                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var7[var2] -= 2;
                        }

                        if (this.enemydir[this.n] == 0) {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                            if (this.enemymove[this.n] == 48) {
                                this.enemydir[this.n] = 1;
                            }
                        } else {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]--;
                            if (this.enemymove[this.n] == 0) {
                                this.enemydir[this.n] = 0;
                            }
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 10:
                        if (this.mby > 128 && this.mby < -16) {
                            break;
                        }

                        var7 = this.enemyy;
                        var2 = this.n;
                        var7[var2] -= 2;
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]++;
                        if (this.enemydata[this.n] == 6) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 13:
                        if (this.enemydir[this.n] == 0) {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                            if (this.enemymove[this.n] == 48) {
                                this.enemydir[this.n] = 1;
                            }
                        } else {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var10002 = var7[var2]--;
                            if (this.enemymove[this.n] == 0) {
                                this.enemydir[this.n] = 0;
                            }
                        }

                        if (this.enemydata[this.n] > 0) {
                            var7 = this.enemydata;
                            var2 = this.n;
                            var10002 = var7[var2]++;
                        }

                        if (this.enemydata[this.n] == 10) {
                            this.enemytype[this.n] = 17;
                            this.enemydata[this.n] = 50;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 14:
                        if (this.enemydata[this.n] > 0 && this.enemymove[this.n] < 16) {
                            var7 = this.enemymove;
                            var2 = this.n;
                            var7[var2] += 2;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 120) {
                            this.enemytype[this.n] = -1;
                        }
                    case 15:
                    default:
                        break;
                    case 16:
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]--;
                        if (this.bossmode == 2) {
                            this.enemydata[this.n] = 0;
                        }

                        if (this.enemydata[this.n] == 0) {
                            this.enemytype[this.n] = 1;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 17:
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]--;
                        if (this.bossmode == 2) {
                            this.enemydata[this.n] = 0;
                        }

                        if (this.enemydata[this.n] == 0) {
                            this.enemytype[this.n] = 13;
                        }

                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 18:
                        var6 = this.enemyx;
                        var2 = this.n;
                        var6[var2] = (byte)(var6[var2] + this.enemymove[this.n]);
                        var7 = this.enemyy;
                        var2 = this.n;
                        var7[var2] += this.enemydata[this.n];
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]++;
                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 19:
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]++;
                        if (this.enemydata[this.n] == 8) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 20:
                        if (this.tut != 0) {
                            break;
                        }

                        if (this.bossbob > 0) {
                            --this.bossbob;
                        }

                        if (this.bosshurt > 0) {
                            --this.bosshurt;
                        }

                        switch (this.bossmode) {
                            case 0:
                                this.bosslistptr = this.bossstart[this.zone];
                                this.bosstime = this.bosslist[this.bosslistptr * 5 + 2];
                                this.bossmode = 1;
                                this.chain1x = 0;
                                this.chain1y = 5;
                                this.chain2x = 0;
                                this.chain2y = 5;
                                this.chainrot1 = 123;
                                this.chainvel = 11;
                                this.chainlen = 24;
                                this.chainfall = 0;
                                this.chainfallg = 0;
                                break;
                            case 1:
                                this.b = this.bosslistptr * 5;
                                this.dx = this.bosslist[this.b + 0];
                                this.dy = this.bosslist[this.b + 1];
                                --this.bosstime;
                                var2 = this.bosstime;
                                var2 = var2 * 20 / this.bosslist[this.b + 2];
                                this.perc = var2;
                                if (this.perc < 3 || this.perc > 16) {
                                    this.dx >>= 1;
                                    this.dy >>= 1;
                                }

                                if (this.perc > 6 && this.perc < 13) {
                                    this.dx += this.dx >> 1;
                                    this.dy += this.dy >> 1;
                                }

                                this.realbossx += this.dx;
                                this.realbossy += this.dy;
                                this.enemyx[this.n] = (byte)(this.realbossx >> 1);
                                this.enemyy[this.n] = this.realbossy >> 1;
                                if (this.bosstime == 0) {
                                    this.bosslistptr = this.bosslist[this.b + 3];
                                    this.bosstime = this.bosslist[this.bosslistptr * 5 + 2];
                                }

                                this.b = this.bosslist[this.bosslistptr * 5 + 4];
                                if (this.b == 1) {
                                    if (this.bosstime < 40) {
                                        this.chain1y = this.bosstime * 64 / 40 + 5;
                                    } else {
                                        this.chain1y = (50 - this.bosstime) * 64 / 10 + 5;
                                    }
                                }

                                if (this.b == 2) {
                                    if (this.chainrot1 > 128) {
                                        --this.chainvel;
                                    }

                                    if (this.chainrot1 < 128) {
                                        ++this.chainvel;
                                    }

                                    this.chainrot1 += this.chainvel;
                                    this.rotatechain(this.chainrot1, 30);
                                }

                                if (this.b == 3) {
                                    if (this.chainlen > 24) {
                                        --this.chainlen;
                                    }

                                    this.chainrot1 += 4;
                                    this.rotatechain(this.chainrot1, this.chainlen);
                                }

                                if (this.b == 4) {
                                    if (this.chainlen < 60) {
                                        ++this.chainlen;
                                    }

                                    this.chainrot1 += 4;
                                    this.rotatechain(this.chainrot1, this.chainlen);
                                }

                                if (this.b == 5) {
                                    if (this.bosstime > 40) {
                                        this.chain1y = (50 - this.bosstime) * 64 / 10 + 5;
                                    } else if (this.bosstime > 20) {
                                        this.chain1y = (this.bosstime - 20) * 64 / 20 + 5;
                                    }

                                    if (this.bosstime > 20) {
                                        if (this.bosstime < 30) {
                                            this.chain2y = (30 - this.bosstime) * 64 / 10 + 5;
                                        }
                                    } else {
                                        this.chain2y = this.bosstime * 64 / 20 + 5;
                                    }
                                }

                                if (this.b == 6) {
                                    this.chainrot1 += 4;
                                    this.rotatechain(this.chainrot1, 40);
                                    this.chain2x = this.chain1x;
                                    this.chain2y = this.chain1y;
                                    this.rotatechain(this.chainrot1 + 128, 40);
                                }

                                if (this.b == 7) {
                                    this.chainrot1 += 2;
                                    if (this.zone == 6) {
                                        ++this.chainrot1;
                                    }

                                    this.rotatechain(-this.chainrot1, 30);
                                    this.chain2x = this.chain1x;
                                    this.chain2y = this.chain1y;
                                    this.rotatechain(this.chainrot1 << 1, 60);
                                }
                                break;
                            case 2:
                                --this.bosstime;
                                if (this.bosstime == 0) {
                                    this.bossmode = 3;
                                }

                                ++this.chainfallg;
                                this.chainfall += this.chainfallg;
                                break;
                            case 3:
                                this.bossbob = 0;
                                var7 = this.enemyy;
                                var2 = this.n;
                                var10002 = var7[var2]--;
                                ++this.chainfallg;
                                this.chainfall += this.chainfallg;
                                if (this.enemyy[this.n] <= -32) {
                                    this.setlevelwon();
                                    this.bossmode = -1;
                                }
                        }

                        int var3;
                        byte var4;
                        int var5;
                        if (this.bosshealth == 0 && (this.random.nextInt() & 255) > 200) {
                            this.vibrawanted = 1;
                            var4 = this.enemyx[this.n];
                            var2 = this.random.nextInt();
                            var3 = this.enemyy[this.n];
                            var5 = this.random.nextInt();
                            this.addsprite(var4 + var2 % 16 - 9, var3 + var5 % 8, 19, 0);
                        }

                        if ((this.random.nextInt() & 255) <= this.bosshealth) {
                            break;
                        }

                        this.h = 256;

                        while(true) {
                            if (this.h <= this.bosshealth) {
                                continue label513;
                            }

                            var4 = this.enemyx[this.n];
                            var3 = this.random.nextInt();
                            var5 = this.enemyy[this.n];
                            var2 = this.random.nextInt();
                            this.addsprite(var4 + 8 + var3 % 4, var5 + var2 % 4, 21, 0);
                            this.h -= 32;
                        }
                    case 21:
                        var6 = this.enemyx;
                        var2 = this.n;
                        var6[var2] = (byte)(var6[var2] + 2);
                        var7 = this.enemyy;
                        var2 = this.n;
                        var7[var2] -= 2;
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]++;
                        if (this.enemydata[this.n] == 12) {
                            this.enemytype[this.n] = -1;
                        }
                        break;
                    case 22:
                        var6 = this.enemyx;
                        var2 = this.n;
                        var6[var2] = (byte)(var6[var2] + this.enemymove[this.n]);
                        var7 = this.enemyy;
                        var2 = this.n;
                        var7[var2] += this.enemydata[this.n];
                        var7 = this.enemydata;
                        var2 = this.n;
                        var10002 = var7[var2]++;
                        if (this.enemyy[this.n] >= this.scrolly + 128) {
                            this.enemytype[this.n] = -1;
                        }
                }
            }
        }

        if (this.tut != 0) {
            this.setsoftkeys(1, 4);
            if (this.key_fire == 1) {
                this.tut = 0;
                this.key_fire = 0;
                this.setsoftkeys(0, 4);
            }

            if (this.key_up > 0) {
                if (this.lineshown > 0) {
                    --this.lineshown;
                }

                this.MenuTimer = 5;
            }

            if (this.key_down > 0) {
                if (this.lineshown + this.maxlines <= this.numstrings) {
                    ++this.lineshown;
                }

                this.MenuTimer = 5;
            }
        } else {
            if (this.MenuTimer > 0) {
                --this.MenuTimer;
            }

            if (this.MenuTimer == 0) {
                if (this.ignorekeys > 0) {
                    --this.ignorekeys;
                }

                if (this.cheatactive[4] != 0 && this.key_hash == 1) {
                    this.setlevelwon();
                }

                if (this.sonicmode != 5 && (this.hurt == 0 || this.inertia > 0) && this.ignorekeys == 0) {
                    if (this.TouchpadVisibleOnOff == 0) {
                        this.sonicacc = -(this.sensor_x * 4.0F);
                        if (tails) { this.sonicacc = -(this.sensor_x * 3.0F); }
                    } else if (this.key_left != 1 && this.key_1 != 1) {
                        if (this.key_right == 1 || this.key_3 == 1) {
                            if (this.sonicacc < 0.0F) {
                                this.sonicacc = 0.0F;
                            } else {
                                this.sonicacc += this.sonicacc + 4.0F;
                                if (tails) { this.sonicacc += this.sonicacc + 3.0F; }
                                if (this.sonicacc > 10.0F) {
                                    this.sonicacc = 10.0F;
                                    if (tails) { this.sonicacc = 8.0F; }
                                }
                            }
                        }
                    } else if (this.sonicacc > 0.0F) {
                        this.sonicacc = 0.0F;
                    } else {
                        this.sonicacc += this.sonicacc - 4.0F;
                        if (tails) { this.sonicacc += this.sonicacc - 3.0F; }
                        if (this.sonicacc < -10.0F) {
                            this.sonicacc = -10.0F;
                            if (tails) { this.sonicacc = -8.0F; }
                        }
                    }
                }

                if (this.sonicacc < 0.0F) {
                    ++this.sonicacc;
                    this.sonicdir = 0;
                } else if (this.sonicacc > 0.0F) {
                    --this.sonicacc;
                    this.sonicdir = 1;
                }

                if (this.sonicacc > -1.0F && this.sonicacc < 1.0F) {
                    this.sonicacc = 0.0F;
                }

                switch (this.sonicmode) {
                    case 0:
                        this.processSonicStanding();
                        break;
                    case 1:
                        this.processSonicJumping();
                        break;
                    case 2:
                        this.processSonicTractored();
                        break;
                    case 3:
                        this.processSonicCrouching();
                        break;
                    case 4:
                        this.processSonicCrouching2();
                        break;
                    case 5:
                        this.processSonicDying();
                }

                float var1 = this.sonicx;
                this.sonicx = var1 + (float)((int)this.sonicacc >> 1);
                if (this.sonicx < 0.0F) {
                    this.sonicx = 0.0F;
                }

                if (this.sonicx > 127.0F) {
                    this.sonicx = 127.0F;
                }
                if (this.sonicy < (float)(this.scrolly + 56)) {
                    this.scrolly = (int)this.sonicy - 56;
                }
                if (this.scrolly < 0) {
                    this.scrolly = 0;
                }
                if (this.sonicy > (float)(this.scrolly + 144) && this.sonicmode != 5) {
                    this.hurt = 0;
                    this.inertia = -10;
                    this.sonicacc = 0.0F;
                    this.sonicmode = 5;
                    this.sonicy = (float)(this.scrolly + 144 - 1);
                    this.setlevelfailed();
                }
            }
            if (this.cheatactive[1] == 0) {
                this.ticks += 2;
                if (this.ticks > 40) {
                    this.ticks = 0;
                    ++this.secs;
                    if (this.secs > 59) {
                        this.secs = 0;
                        if (this.mins < 9) {
                            ++this.mins;
                        } else {
                            this.mins = 9;
                            this.secs = 59;
                            this.setlevelfailed();
                        }
                    }
                }
            }
        }
        if (this.levelover == 1) {
            --this.levelovertime;
            if (this.levelovertime == 0) {
                this.LoopModeWanted = 14;
            }
        }
        if (this.levelover == 2) {
            --this.levelovertime;
            if (this.levelovertime == 0) {
                this.LoopModeWanted = 6;
                if (!Paying.isPayed(0) && this.lives == 0) {
                    byte var8 = Paying.getStatus(0);
                    if (var8 < 5) {
                        Paying.setStatus(0, (byte)(var8 + 1));
                    }
                    this.m_nPayPoint = 1;
                    this.setPaying(0);
                }
            }
        }
    }

    private void processSonicDying() {
        this.sonicy += this.inertia;
        this.inertia++;
        if (this.inertia > 7) {
            this.inertia = 7;
        }
    }

    private void processSonicCrouching2() {
        this.sonicmode = 1;
        this.sonicy -= 4.0f;
    }

    private void processSonicCrouching() {
        this.sonicacc = 0.0f;
        this.sonicmode = 4;
        this.ignorekeys = 1;
    }

    private void processSonicStanding() {
        initjump();
        this.firstjump = 1;
    }

    private void processSonicJumping() {
        if (this.superjump == 1 && shadow) {
        this.inertia = this.inertia + 2;
        }
        if (this.inertia >= -6 && this.inertia <= 8 && this.superjump == 0 && this.hurt == 0 && this.KEY_RELEASED && (this.key_1 == 1 || this.key_2 == 1 || this.key_3 == 1 || this.key_fire == 1)) {
            this.key_fire = 0;
            this.key_2 = 0;
            if (tails && !tailsfly) {
        this.superjump = 0;
        tailsfly = true;
        } else if (!tails || (tails && tailsfly)) {
        this.superjump = 1;
        tailsfly = false;
        } else if (shadow && !shadowt) {
        this.superjump = 1;
        shadowt = true;
        } else if (!shadow || (shadow && shadowt)) {
        this.superjump = 1;
        shadowt = false;
        }
            //this.superjump = 1;
            this.inertia = -6;
            this.sfxwanted[7] = 1;
            if (this.key_1 == 1 || this.key_3 == 1) {
                this.KEY_RELEASED = false;
            }
        }
        if (!shadow) {
        this.sonicy += this.inertia;
        } else {
        this.sonicy += this.inertia - 2;
        }
        this.inertia++;
        if (this.inertia > 7) {
            this.inertia = 7;
        }
        testcollision();
    }

    private void processSonicTractored() {
        addsparkle(10);
        this.sonicy -= 4.0f;
        this.tractortime--;
        if (this.tractortime == 0) {
            this.sonicmode = 1;
            this.hurt = 0;
            this.inertia = 0;
            addsprite(GFX_ZONE_2_ENEMY_B, -48, 20, this.random.nextInt() & 1);
            this.realbossx = 128;
            this.realbossy = -96;
            this.bosshurt = 0;
            this.bosshealth = 256;
            this.bossbob = 0;
            this.bossmode = 0;
        }
        testringonlycollision();
    }

    void addsparkle(int s) {
        int tw = this.framelist[GFX_BLUE_BONUS_GO_A] + (this.tractortime >> 1);
        int tx = (int) this.sonicx;
        int th = (((int) this.sonicy) - this.scrolly) + (this.tractortime % 16);
        for (int i = 0; i < s; i++) {
            addsprite((this.random.nextInt() % (tw >> 1)) + tx, this.scrolly + (((this.random.nextInt() % th) + th) >> 1), 10, 0);
        }
    }

    boolean stillbouncing(int s) {
        if (this.levelover != 1 && this.bossmode != 2 && this.bossmode != 3) {
            return true;
        }
        this.sonicmode = 6;
        this.sonicacc = 0.0f;
        this.inertia = 0;
        this.sonicy = this.enemyy[s];
        this.ignorekeys = 999;
        this.wonattatch = s;
        this.wonattatchx = ((int) this.sonicx) - (this.enemyx[s] + this.enemymove[s]);
        return false;
    }

    void hitball(int tdx, int tdy) {
        int tdx2 = ((int) this.sonicx) - tdx;
        int tdy2 = ((int) this.sonicy) - tdy;
        if (this.gzone == 0 || this.gzone == 3) {
            if (tdy2 >= -6 && tdy2 < 16 && tdx2 >= -16 && tdx2 < 16) {
                DamageSonic(tdx2);
                return;
            }
            return;
        }
        if (tdy2 >= -6 && tdy2 < 20 && tdx2 >= -10 && tdx2 < 10) {
            DamageSonic(tdx2);
        }
    }

    void testcollision() {
        if (this.levelover != 2) {
            this.n = 0;
            while (this.n < this.si) {
                this.type = this.enemytype[this.n];
                if (this.type >= 0) {
                    this.mby = this.enemyy[this.n] - this.scrolly;
                    if (this.mby <= 128 || this.mby >= -16) {
                        this.dy = ((int) this.sonicy) - this.enemyy[this.n];
                        this.dx = ((int) this.sonicx) - this.enemyx[this.n];
                        switch (this.type) {
                            case 0:
                            case 9:
                                if (this.dy - this.inertia < 2 && this.dy >= 0 && this.dy < 8) {
                                    this.dx -= this.enemymove[this.n];
                                    if (this.dx >= -6 && this.dx < 30) {
                                        this.sonicy = this.enemyy[this.n];
                                        this.n2 = this.n - 1;
                                        if (stillbouncing(this.n)) {
                                            this.sfxwanted[4] = 1;
                                            this.enemydata[this.n] = 4;
                                            initjump();
                                        }
                                        if (this.n2 >= 0 && this.enemytype[this.n2] == 14 && this.enemydata[this.n2] == 0) {
                                            this.sfxwanted[0] = 1;
                                            this.enemydata[this.n2] = 1;
                                            this.RestartPoint = 1;
                                            this.RestartPointx = this.enemyx[this.n2] + 4;
                                            this.RestartPointy = this.enemyy[this.n2] + 8;
                                            this.RestartSecs = this.secs;
                                            this.RestartMins = this.mins;
                                            this.RestartTicks = this.ticks;
                                        }
                                        if (this.ssely == 0 && this.tut2done == 0 && this.scrolly < 1563 && sonic && (!demoStarted || DemoFurikan)) {
                                            settut(GFX_ZONE_3_ENEMY_B_FLIP);
                                            this.tut2done = 1;
                                            this.tutdone = 1;
                                            setsoftkeys(1, 4);
                                            break;
                                        }
                                    }
                                }
                                break;
                            case 1:
                            case 13:
                                if (this.inertia >= 0 && this.enemydata[this.n] == 0 && this.dy - this.inertia < 2 && this.dy >= 0 && this.dy < 8) {
                                    this.dx -= this.enemymove[this.n];
                                    if (this.dx >= -6 && this.dx < 30) {
                                        if (stillbouncing(this.n)) {
                                            this.sfxwanted[4] = 1;
                                            this.enemydata[this.n] = 1;
                                            initjump();
                                        }
                                        this.sonicy = this.enemyy[this.n];
                                        break;
                                    }
                                }
                                break;
                            case 2:
                            case 12:
                                if (this.inertia >= 0) {
                                    if (this.dy - this.inertia < 2 && this.dy >= 0 && this.dy < 8) {
                                        this.dx -= this.enemymove[this.n];
                                        if (this.dx >= -6 && this.dx < 30 && stillbouncing(this.n)) {
                                            this.sfxwanted[4] = 1;
                                            this.enemydata[this.n] = 4;
                                            initjump();
                                            break;
                                        }
                                    }
                                } else if (this.dy >= 12 && this.dy < 20) {
                                    this.dx -= this.enemymove[this.n];
                                    if (this.dx >= 0 && this.dx < 20) {
                                        DamageSonic(this.dx - 12);
                                        break;
                                    }
                                }
                                break;
                            case 3:
                                if (this.enemydata[this.n] == 0 && this.dy >= 0 && this.dy < 24 && this.dx > -12 && this.dx < 12) {
                                    this.enemydata[this.n] = 1;
                                    //if (!demoStarted || demoStarted && this.rings < 49) {
                                    this.rings++;
                                    if (this.rings >= eventrings && eventrings != 0 && (character.equals(eventcharacter) || eventcharacter.equals("any")) && this.lsely == eventlevel && this.lselx == eventlevel2) {
                                    eventrings2 = eventrings;
                                    SaveGame();
                                    }
                                    //}
                                    this.sfxwanted[2] = 1;
                                    if (this.rings == this.bonusat) {
                                    if (!demoStarted) {
                                        this.bonustext = localText[GFX_ANTENNA_BASE];
                                        this.bonustime = 20;
                                        this.lives++;
                                        this.bonusat += GFX_SONIC_DEAD_FLIP;
                                        }
                                        break;
                                    } else {
                                        break;
                                    }
                                }
                                break;
                            case SOUND_PLAY_STORED:
                            case 11:
                                if (this.inertia >= 0 && this.dy - this.inertia < 2 && this.dy >= 0 && this.dy < 8) {
                                    this.dx -= this.enemymove[this.n];
                                    if (this.dx >= -6 && this.dx < 30) {
                                        this.sonicy = this.enemyy[this.n];
                                        if (stillbouncing(this.n)) {
                                            this.sfxwanted[5] = 1;
                                            this.enemydata[this.n] = 8;
                                            initjump();
                                            this.superjump = 0;
                                            this.inertia = -13;
                                            break;
                                        } else {
                                            break;
                                        }
                                    }
                                }
                                break;
                            case 5:
                                if (this.enemydata[this.n] == 0 && this.dy >= 0 && this.dy < 14 && this.dx > -18 && this.dx < 18) {
                                    this.enemydata[this.n] = 1;
                                    calchoopbonus(this.dx);
                                    this.sfxwanted[0] = 1;
                                    break;
                                }
                                break;
                            case 6:
                                if (this.enemydata[this.n] == 0 && this.inertia <= 0 && this.dy >= 0 && this.dy < 14 && this.dx > -18 && this.dx < 18) {
                                    this.sonicmode = 1;
                                    this.superjump = 0;
                                    this.hurt = 0;
                                    this.inertia = INERTIA_BLUE_BONUS_FLING;
                                    this.enemydata[this.n] = 1;
                                    calchoopbonus(this.dx);
                                    this.sfxwanted[6] = 1;
                                    break;
                                }
                                break;
                            case 7:
                                if (this.enemydata[this.n] == 0) {
                                    this.dx -= this.enemymove[this.n];
                                    if (this.dy >= this.nmetop[this.gzone] && this.dy < this.nmebot[this.gzone] && this.dx > (-this.nmehalfwid[this.gzone]) && this.dx < this.nmehalfwid[this.gzone]) {
                                        if (sonicsafe() == 0) {
                                            DamageSonic(this.dx);
                                            break;
                                        } else {
                                            this.enemydata[this.n] = 1;
                                            break;
                                        }
                                    }
                                } else {
                                    break;
                                }
                                break;
                            case 8:
                                if (this.enemydata[this.n] != 0) {
                                    break;
                                } else if (this.sect < 2) {
                                    if (this.dy > -16 && this.dy < 8) {
                                        setlevelwon();
                                        break;
                                    }
                                } else if (this.dy >= -16 && this.dy < 8) {
                                    this.enemydata[this.n] = 1;
                                    this.sonicmode = 2;
                                    this.si = MAXSPRITES;
                                    this.tractortime = GFX_GREEN_BONUS_GO_A;
                                    break;
                                }
                                break;
                            case 20:
                                if (this.bosshealth > 0 && this.hurt == 0 && this.bosshurt == 0 && this.bossmode != 0) {
                                    if (this.gzone == 3) {
                                        hitball(this.enemyx[this.n] - 14, this.enemyy[this.n] + 10 + this.chain1y);
                                        hitball(this.enemyx[this.n] + 14, this.enemyy[this.n] + 10 + this.chain2y);
                                    } else {
                                        hitball(this.enemyx[this.n] + this.chain1x, this.enemyy[this.n] + 10 + this.chain1y);
                                        if (this.gzone >= 4) {
                                            hitball(this.enemyx[this.n] + this.chain2x, this.enemyy[this.n] + 10 + this.chain2y);
                                        }
                                    }
                                    if (this.dy >= 0 && this.dy < 30 && this.dx > -22 && this.dx < 22) {
                                        this.sonicacc = -this.sonicacc;
                                        if (sonicsafe() == 0) {
                                            DamageSonic(this.dx);
                                        } else {
                                            this.vibrawanted = 2;
                                            addsprite((((int) this.sonicx) - (this.dx >> 1)) - 8, (((int) this.sonicy) - (this.dy >> 1)) - 8, 19, 0);
                                            for (int s = 256; s >= this.bosshealth; s -= 32) {
                                                AddShard();
                                            }
                                            this.sfxwanted[3] = 1;
                                            this.bd = GFX_ZONE_2_ENEMY_B;
                                            if (this.zone == 1) {
                                                this.bd = GFX_GREEN_BONUS_FRONT;
                                            }
                                            if (this.zone == 6) {
                                                this.bd = GFX_GREEN_BONUS_FRONT;
                                            }
                                            if (this.superjump != 0) {
                                                this.bd = (this.bd * 3) / 2;
                                            }
                                            this.bosshealth -= this.bd;
                                            if (this.bosshealth <= 0) {
                                                this.bosshealth = 0;
                                                this.score += 10000;
                                                this.bossmode = 2;
                                                this.bosstime = 30;
                                            } else {
                                                this.bosshurt = 24;
                                            }
                                        }
                                        if (this.inertia > 0) {
                                            this.bossbob = 2;
                                            this.inertia = -9;
                                            break;
                                        } else {
                                            break;
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
                this.n++;
            }
        }
    }

    void testringonlycollision() {
        this.n = 0;
        while (this.n < MAXSPRITES) {
            if (this.enemytype[this.n] == 3) {
                this.dy = ((int) this.sonicy) - this.enemyy[this.n];
                this.dx = ((int) this.sonicx) - this.enemyx[this.n];
                if (this.enemydata[this.n] == 0 && this.dy >= 0 && this.dy < 24 && this.dx > -12 && this.dx < 12) {
                    this.enemydata[this.n] = 1;
                    //if (!demoStarted || demoStarted && this.rings < 49) {
                    this.rings++;
                    if (this.rings >= eventrings && eventrings != 0 && (character.equals(eventcharacter) || eventcharacter.equals("any")) && this.lsely == eventlevel && this.lselx == eventlevel2) {
                                    eventrings2 = eventrings;
                                    SaveGame();
                                    }
                    //}
                    this.sfxwanted[2] = 1;
                    if (this.rings == this.bonusat) {
                    if (!demoStarted) {
                        this.bonustext = localText[GFX_ANTENNA_BASE];
                        this.bonustime = 20;
                        this.lives++;
                        this.bonusat += GFX_SONIC_DEAD_FLIP;
                        }
                    }
                }
            }
            this.n++;
        }
    }

    void calchoopbonus(int dx) {
        if (dx < 0) {
            dx = -dx;
        }
        int dx2 = (5 - ((dx * 4) / 17)) * 200;
        this.bonustext = String.valueOf(localText[GFX_ANTENNA_ON]) + " " + dx2;
        this.score += dx2;
        this.bonustime = 20;
    }

    int sonicsafe() {
        if (this.hurt == 1) {
            return 0;
        }
        if (this.cheatactive[5] == 1) {
            return 1;
        }
        return (this.sonicmode != 1 || this.inertia < -13 || this.inertia > 3) ? 0 : 1;
    }

    void DamageSonic(int dir) {
        if (this.hurt == 0 && this.invulnerable == 0 && this.cheatactive[5] != 1) {
            int d = 10;
            if (this.rings < 10) {
                d = this.rings;
            }
            this.rings -= d;
            for (int n = 0; n < d; n++) {
                addsprite((int) this.sonicx, (int) this.sonicy, 18, this.random.nextInt());
                if (this.lastadded != -1) {
                    this.enemymove[this.lastadded] = this.random.nextInt() % 5;
                    int r = this.random.nextInt() % 9;
                    if (r < 0) {
                        r = 0 - r;
                    }
                    this.enemydata[this.lastadded] = -r;
                }
            }
            this.superjump = 0;
            this.sonicacc = -6.0f;
            if (dir > 0) {
                this.sonicacc = 6.0f;
            }
            if (d == 0) {
                this.inertia = -9;
                this.sonicmode = 5;
                setlevelfailed();
            } else {
                this.invulnerable = GFX_SONIC_DEAD_FLIP;
                this.hurt = 1;
                this.inertia = -6;
                this.sonicmode = 3;
                this.sfxwanted[1] = 1;
            }
        }
    }

    void AddShard() {
        addsprite((int) this.sonicx, (int) this.sonicy, 22, this.random.nextInt() & 3);
        if (this.lastadded != -1) {
            this.enemymove[this.lastadded] = this.random.nextInt() % 5;
            int r = this.random.nextInt() % 9;
            if (r < 0) {
                r = 0 - r;
            }
            this.enemydata[this.lastadded] = -r;
        }
    }

    public void addsprite(int x, int y, int type, int dir) {
        for (int n = 0; n < MAXSPRITES; n++) {
            if (this.enemytype[n] == -1) {
                this.enemyx[n] = (byte) x;
                this.enemyy[n] = y;
                this.enemytype[n] = (byte) type;
                this.enemydir[n] = (byte) dir;
                this.enemymove[n] = 0;
                this.enemydata[n] = 0;
                this.lastadded = n;
                return;
            }
        }
        this.lastadded = -1;
    }

    void initgame() {
        this.lives = 2;
        this.RestartPoint = 0;
    }

    public void InitRoom() {
        this.score = 0;
        this.rings = 0;
        this.bonusat = GFX_SONIC_DEAD_FLIP;
        this.levelover = 0;
        this.bonustime = 0;
        this.sonicmode = 0;
        this.hurt = 0;
        this.sonicacc = 0.0f;
        this.invulnerable = 0;
        this.srot = 128;
        this.spic = 0;
        this.bossmode = -1;
        this.ticks = 0;
        this.mins = 0;
        this.secs = 0;
        this.zonemapptr = zonemap1;
        if (this.gzone == 1) {
            this.zonemapptr = zonemap2;
        }
        if (this.gzone == 2) {
            this.zonemapptr = zonemap3;
        }
        if (this.gzone == 3) {
            this.zonemapptr = zonemap4;
        }
        if (this.gzone == 4) {
            this.zonemapptr = zonemap5;
        }
        if (this.gzone == 5) {
            this.zonemapptr = zonemap6;
        }
        this.n = 0;
        while (this.n < MAXSPRITES) {
            this.enemytype[this.n] = -1;
            this.n++;
        }
        LoadMap();
        if (this.ssely == 0 && sonic && (!demoStarted || DemoFurikan)) {
            if (this.TouchpadVisibleOnOff == 0) {
                settut(GFX_ZONE_4_ENEMY_A);
            } else {
                settut(GFX_ZONE_4_ENEMY_B);
            }
        }
        this.tut2done = 0;
        if (this.RestartPoint != 0) {
            this.tut = 0;
            this.tut2done = 1;
            this.sonicx = this.RestartPointx;
            this.sonicy = this.RestartPointy;
            this.scrolly = ((int) this.sonicy) - GFX_CRUSHER;
            this.scrollywanted = this.scrolly;
            this.secs = this.RestartSecs;
            this.mins = this.RestartMins;
            this.ticks = this.RestartTicks;
            if (this.RestartPoint == 2) {
                this.sonicmode = 2;
                this.si = MAXSPRITES;
                this.tractortime = GFX_GREEN_BONUS_GO_A;
                addsparkle(30);
            }
        }
        if (this.sonicx > 63.0f) {
            this.sonicdir = 0;
        } else {
            this.sonicdir = 1;
        }
        int y = 0;
        this.x = 0;
        while (this.x < MAXSPRITES) {
            if (this.enemytype[this.x] != -1) {
                y++;
            }
            this.x++;
        }
    }
    
    /*private void loadMapFromAssets() {
    try {
        // Define tamanho do realmap
        int totalSize = 0;
        for (int size : roomsize) totalSize += size;
        this.realmap = new byte[totalSize];

        // Define nomes das salas
        int index = 0;
        for (int j = 1; j <= 7; j++) {
            for (int i = 1; i <= 3; i++) {
                rooms[index] = "Room " + j + "-" + i;
                index++;
            }
        }

        InputStream is = activity.getAssets().open("decoded_map.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        int roomId = -1;
        int roomOffset = 0;
        int roomMaxBytes = 0;
        int currentWriteIndex = 0;

        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty()) continue;

            boolean isRoomLine = false;
            for (int i = 0; i < rooms.length; i++) {
                if (rooms[i].equals(line)) {
                    roomId = i;
                    roomOffset = roomindex[roomId];
                    roomMaxBytes = roomsize[roomId];
                    currentWriteIndex = 0;
                    isRoomLine = true;
                    break;
                }
            }
            if (isRoomLine) continue;
            if (roomId < 0) continue;

            String[] parts = line.split(",");
            if (parts.length != 3) continue;

            int tipo = -1, x = -1, y = -1;
            try {
                for (String part : parts) {
                    part = part.trim();
                    if (part.startsWith("Type=")) tipo = Integer.parseInt(part.substring(5));
                    else if (part.startsWith("X=")) x = Integer.parseInt(part.substring(2));
                    else if (part.startsWith("Y=")) y = Integer.parseInt(part.substring(2));
                }
            } catch (NumberFormatException e) {
                continue;
            }

            if (tipo < 0 || x < 0 || x > 15 || y < 0 || y > 255) continue;
            if (currentWriteIndex + 2 > roomMaxBytes) continue;

            int v2 = (tipo << 4) | (x & 15);
            int v = v2 - 128;

            realmap[roomOffset + currentWriteIndex] = (byte) v;
            realmap[roomOffset + currentWriteIndex + 1] = (byte) y;
            currentWriteIndex += 2;
        }

        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Error loading asset map.");
    }
}*/
    
    public void loadMapFromAssets(boolean asTxt, ByteArrayInputStream On2) {
    if (OnlineIn == null) {
    for (int i = 0; i < life.length; i++) {
    life[i] = 2;
    }
    }
    if (!asTxt && OnlineIn != null) {
    this.lives = 2;
    try (BufferedInputStream bis = new BufferedInputStream(OnlineIn)) {
        bis.mark(4096);
        DataInputStream dis = new DataInputStream(bis);

        int numRooms = dis.readInt();
        roomsize = new int[numRooms];
            for (int i = 0; i < numRooms; i++) {
                roomsize[i] = dis.readInt();
            }
            
            int runningIndex = 0;
            roomindex = new int[numRooms];
            for (int i = 0; i < numRooms; i++) {
                roomindex[i] = runningIndex;
                runningIndex += roomsize[i];
            }
            byte[] rawmap = new byte[runningIndex];
            dis.readFully(rawmap);
            int totalSize = runningIndex / 3 * 2;
            realmap = new byte[totalSize];
            
            EdgePicsBackup = new DeviceImage[EdgePics.length];
for (int i = 0; i < EdgePics.length; i++) {
    EdgePicsBackup[i] = EdgePics[i];
}

BlockPicsBackup = new DeviceImage[BlockPics.length];
for (int i = 0; i < BlockPics.length; i++) {
    BlockPicsBackup[i] = BlockPics[i];
}

if (ExtrasPic == null) ExtrasPic = new DeviceImage[21];
ExtrasPicBackup = new DeviceImage[ExtrasPic.length];
for (int i = 0; i < ExtrasPic.length; i++) {
    ExtrasPicBackup[i] = ExtrasPic[i];
}

StripPicsBackup = new DeviceImage[StripPics.length];
for (int i = 0; i < StripPics.length; i++) {
    StripPicsBackup[i] = StripPics[i];
}

TilesPicsBackup = new DeviceImage[TilesPics.length];
for (int i = 0; i < TilesPics.length; i++) {
    TilesPicsBackup[i] = TilesPics[i];
}
            
            BlockPics2 = new DeviceImage[21];
            EdgePics2 = new DeviceImage[21];
            ExtrasPic2 = new DeviceImage[21];
            StripPics2 = new DeviceImage[21];
            TilesPics2 = new DeviceImage[21];
            
            for (int i = 0; i < numRooms; i++) {
            life[i] = dis.readInt();
            String name = dis.readUTF();
            if (!name.isEmpty()) BlockPics2[i] = new DeviceImage(name);
            else BlockPics2[i] = null;
            name = dis.readUTF();
            if (!name.isEmpty()) EdgePics2[i] = new DeviceImage(name);
            else EdgePics2[i] = null;
            name = dis.readUTF();
            if (!name.isEmpty()) ExtrasPic2[i] = new DeviceImage(name);
            else ExtrasPic2[i] = null;
            name = dis.readUTF();
            if (!name.isEmpty()) StripPics2[i] = new DeviceImage(name);
            else StripPics2[i] = null;
            name = dis.readUTF();
            if (!name.isEmpty()) TilesPics2[i] = new DeviceImage(name);
            else TilesPics2[i] = null;
            stagename[i] = dis.readUTF();
            actNumber[i] = dis.readInt();
            currentmusic[i] = dis.readUTF();

if (BlockPics2[i] != null) {
    BlockPics[i] = BlockPics2[i];
}

if (EdgePics2[i] != null) {
    EdgePics[i] = EdgePics2[i];
}

if (ExtrasPic2[i] != null) {
    ExtrasPic[i] = ExtrasPic2[i];
}

if (StripPics2[i] != null) {
    StripPics[i] = StripPics2[i];
}

if (TilesPics2[i] != null) {
    TilesPics[i] = TilesPics2[i];
}
        }

        int originalIndex = 0;
        int i2 = 0;
        for (int room = 0; room < numRooms; room++) {
            int objectCount = roomsize[room] / 3;
            roomsize[room] = objectCount * 2;
            roomindex[room] = i2;
            int roomOffset = roomindex[room];
            i2 += roomsize[room];

            int currentWriteIndex = 0;
            for (int obj = 0; obj < objectCount; obj++) {
                int v = rawmap[originalIndex++];
                int yHigh = rawmap[originalIndex++] & 0xFF;
                int yLow = rawmap[originalIndex++] & 0xFF;

                /*int v2 = v + 128;
                int tipo = v2 >> 4;
                int x = v2 & 15;*/

                int y = (yHigh << 8) | yLow;

                if (y > 255) y -= 256;

                if (currentWriteIndex + 2 > roomsize[room]) {
                    continue;
                }

                /*v2 = (tipo << 4) | (x & 15);
                v = v2 - 128;*/

                realmap[roomOffset + currentWriteIndex] = (byte) v;
                realmap[roomOffset + currentWriteIndex + 1] = (byte) y;

                currentWriteIndex += 2;
            }
        }
        this.LoopModeWanted = 11;
        this.LoopModeWanted = 5;

    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Error loading binary map: " + e.getMessage());
    }
    return;
    }
    try {
        // Inicializa nomes das salas
        int index = 0;
        for (int j = 1; j <= 7; j++) {
            for (int i = 1; i <= 3; i++) {
                rooms[index] = "Room " + j + "-" + i;
                index++;
            }
        }

        int[] sizes = new int[rooms.length];
        int[] roomOffsets = new int[rooms.length];

        try (BufferedReader sizeReader = new BufferedReader(new InputStreamReader(OnlineIn != null ? OnlineIn : activity.getAssets().open("decoded_map.txt")))) {
            String line;
            int roomId = -1;
            EdgePicsBackup = new DeviceImage[EdgePics.length];
for (int i = 0; i < EdgePics.length; i++) {
    EdgePicsBackup[i] = EdgePics[i];
}

BlockPicsBackup = new DeviceImage[BlockPics.length];
for (int i = 0; i < BlockPics.length; i++) {
    BlockPicsBackup[i] = BlockPics[i];
}

if (ExtrasPic == null) ExtrasPic = new DeviceImage[21];
ExtrasPicBackup = new DeviceImage[ExtrasPic.length];
for (int i = 0; i < ExtrasPic.length; i++) {
    ExtrasPicBackup[i] = ExtrasPic[i];
}

StripPicsBackup = new DeviceImage[StripPics.length];
for (int i = 0; i < StripPics.length; i++) {
    StripPicsBackup[i] = StripPics[i];
}

TilesPicsBackup = new DeviceImage[TilesPics.length];
for (int i = 0; i < TilesPics.length; i++) {
    TilesPicsBackup[i] = TilesPics[i];
}
            
            BlockPics2 = new DeviceImage[21];
            EdgePics2 = new DeviceImage[21];
            ExtrasPic2 = new DeviceImage[21];
            StripPics2 = new DeviceImage[21];
            TilesPics2 = new DeviceImage[21];
            while ((line = sizeReader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) continue;

                // Identifica sala
                for (int i = 0; i < rooms.length; i++) {
                    if (rooms[i].equals(line)) {
                        roomId = i;
                        break;
                    }
                }
                if (roomId < 0) continue;

                if (line.startsWith("RoomSize=")) {
                    sizes[roomId] = (int) (Integer.parseInt(line.substring(9)) / 1.5);
                }
                
                if (line.startsWith("Life Count=") && roomId >= 0) {
                life[roomId] = Integer.parseInt(line.substring(11));
                continue;
            }
            
            if (line.startsWith("BlockPics=") && roomId >= 0) {
                BlockPics2[roomId] = new DeviceImage(line.substring(10).trim());
                continue;
            }
            
            if (line.startsWith("EdgePics=") && roomId >= 0) {
                EdgePics2[roomId] = new DeviceImage(line.substring(9).trim());
                continue;
            }
            
            if (line.startsWith("ExtrasPic=") && roomId >= 0) {
                ExtrasPic2[roomId] = new DeviceImage(line.substring(10).trim());
                continue;
            }
            
            if (line.startsWith("StripPics=") && roomId >= 0) {
                StripPics2[roomId] = new DeviceImage(line.substring(10).trim());
                continue;
            }
            
            if (line.startsWith("TilesPics=") && roomId >= 0) {
                TilesPics2[roomId] = new DeviceImage(line.substring(10).trim());
                continue;
            }
            
            if (line.startsWith("StageName=") && roomId >= 0) {
                stagename[roomId] = line.substring(10).trim();
                continue;
            }
            
            if (line.startsWith("StageAct=") && roomId >= 0) {
                actNumber[roomId] = Integer.parseInt(line.substring(9));
                continue;
            }
            
            currentmusic[roomId] = null;
            
            if (line.startsWith("Music=") && roomId >= 0) {
                currentmusic[roomId] = URLDecoder.decode(line.substring(6).trim(), "utf-8");
                continue;
            }
            
                if (BlockPics2[roomId] != null) {
    BlockPics[roomId] = BlockPics2[roomId];
}

if (EdgePics2[roomId] != null) {
    EdgePics[roomId] = EdgePics2[roomId];
}

if (ExtrasPic2[roomId] != null) {
    ExtrasPic[roomId] = ExtrasPic2[roomId];
}

if (StripPics2[roomId] != null) {
    StripPics[roomId] = StripPics2[roomId];
}

if (TilesPics2[roomId] != null) {
    TilesPics[roomId] = TilesPics2[roomId];
}
            }
        }

        // Calcula offsets cumulativos
        int offset = 0;
        for (int i = 0; i < rooms.length; i++) {
            roomOffsets[i] = offset;
            offset += sizes[i];
        }

        this.roomsize = sizes;
        this.roomindex = roomOffsets;
        this.realmap = new byte[offset];

        // Agora lê os dados e escreve no realmap
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(On2 != null ? On2 : activity.getAssets().open("decoded_map.txt")))) {
            String line;
            int roomId = -1;
            int roomOffset = 0;
            int roomMaxBytes = 0;
            int currentWriteIndex = 0;

            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) continue;

                boolean isRoomLine = false;
                for (int i = 0; i < rooms.length; i++) {
                    if (rooms[i].equals(line)) {
                        roomId = i;
                        roomOffset = roomOffsets[roomId];
                        roomMaxBytes = sizes[roomId];
                        currentWriteIndex = 0;
                        isRoomLine = true;
                        break;
                    }
                }
                if (isRoomLine) continue;
                if (roomId < 0) continue;

                String[] parts = line.split(",");
                if (parts.length != 3) continue;

                int tipo = -1, x = -1, y = -1;
                try {
                    for (String part : parts) {
                        part = part.trim();
                        if (part.startsWith("Type=")) tipo = Integer.parseInt(part.substring(5));
                        else if (part.startsWith("X=")) x = Integer.parseInt(part.substring(2));
                        else if (part.startsWith("Y=")) y = Integer.parseInt(part.substring(2));
                    }
                } catch (NumberFormatException e) {
                    continue;
                }

                if (tipo < 0 || x < 0 || x > 15 || y < 0) continue;

                if (y > 255) y -= 256;

                if (currentWriteIndex + 2 > roomMaxBytes) continue;

                int v2 = (tipo << 4) | (x & 15);
                int v = v2 - 128;

                realmap[roomOffset + currentWriteIndex] = (byte) v;
                realmap[roomOffset + currentWriteIndex + 1] = (byte) y;
                currentWriteIndex += 2;
            }
            if (OnlineIn != null) {
        this.LoopModeWanted = 11;
        this.LoopModeWanted = 5;
        }
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Error loading asset map.");
    }
}

    public void LoadMap() {
        this.maxy = 0;
        this.yoff = 0;
        this.p = this.roomindex[this.currentmap];
        this.si = this.roomsize[this.currentmap] / 2;
        if (this.cheatactive[1] == 1) {
            this.si = GFX_HUD_LIVES;
            this.rings = GFX_SONIC_DEAD_FLIP;
        }
        for (int x = 0; x < this.si; x++) {
            int ch = this.realmap[this.p];
            this.p++;
            int ch2 = this.realmap[this.p];
            this.p++;
            AddEnemy(ch, ch2);
        }
    }

    void AddEnemy(int v, int y) {
        int v2 = v + 128;
        int t = v2 >> 4;
        int x = v2 & 15;
        if (y < 0) {
            y += 256;
        }
        if (y < this.maxy) {
            this.yoff += 256;
        }
        this.maxy = y;
        int x2 = (x * 8) + 4;
        int y2 = (y + this.yoff) * 8;
        if (t == 3) {
            x2 += 4;
        }
        if (t == 5) {
            x2 += 4;
        }
        if (t == 6) {
            x2 += 4;
        }
        if (t == 7) {
            x2 += 4;
        }
        addsprite(x2, y2, t, 0);
        if (t == 0 || t == 8) {
            this.sonicx = x2 + 12;
            if (t == 8) {
                int i = x2 - 12;
                y2 += 32;
            }
            this.sonicy = y2;
            this.scrolly = y2 - GFX_CRUSHER;
            this.scrollywanted = this.scrolly;
            this.maxscrolly = this.scrolly;
        }
        if (t == 14 && this.RestartPoint != 0 && this.RestartPointy == y2 + 8) {
            this.enemymove[this.lastadded] = 16;
        }
        int r = this.random.nextInt() % GFX_EXPLOSION_D;
        if (r < 0) {
            r += GFX_EXPLOSION_D;
        }
        if (t == 7) {
            int[] iArr = this.enemymove;
            int i2 = this.lastadded;
            iArr[i2] = iArr[i2] + r;
        }
        if (t == 9) {
            int[] iArr2 = this.enemymove;
            int i3 = this.lastadded;
            iArr2[i3] = iArr2[i3] + r;
        }
        if (t == 11) {
            int[] iArr3 = this.enemymove;
            int i4 = this.lastadded;
            iArr3[i4] = iArr3[i4] + r;
        }
        if (t == 12) {
            int[] iArr4 = this.enemymove;
            int i5 = this.lastadded;
            iArr4[i5] = iArr4[i5] + r;
        }
        if (t == 13) {
            int[] iArr5 = this.enemymove;
            int i6 = this.lastadded;
            iArr5[i6] = iArr5[i6] + r;
        }
    }
    
    private void LoadGame() {
    if (!Paying.isPayed(0)) return;
    Context context = activity;
    File internalFile = new File(activity.getFilesDir(), SAVEFILE);
    if (internalFile.exists()) {
        if (Build.VERSION.SDK_INT >= 30) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DISPLAY_NAME, SAVEFILE);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "application/octet-stream");
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, "Documents/SonicJump/");
            values.put(MediaStore.MediaColumns.IS_PENDING, 1);

            ContentResolver resolver = context.getContentResolver();
            //Uri collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
            Uri itemUri = resolver.insert(MediaStore.Files.getContentUri("external"), values);
            if (itemUri != null) {
                resolver.delete(itemUri, null, null);
            }

            if (itemUri != null) {
                try (OutputStream out = resolver.openOutputStream(itemUri);
                     FileInputStream inputFile = new FileInputStream(internalFile)) {

                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = inputFile.read(buffer)) != -1) {
                        out.write(buffer, 0, bytesRead);
                    }

                    values.clear();
                    values.put(MediaStore.MediaColumns.IS_PENDING, 0);
                    resolver.update(itemUri, values, null, null);

                    internalFile.delete();
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
        } else {
            File publicDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "SonicJump");
            if (!publicDir.exists()) publicDir.mkdirs();

            File publicFile = new File(publicDir, SAVEFILE);

            try (FileInputStream inputFile = new FileInputStream(internalFile);
                 FileOutputStream out = new FileOutputStream(publicFile)) {

                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputFile.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }

                if (publicFile.exists()) internalFile.delete();

            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    }

    InputStream in = null;
    try {
        if (Build.VERSION.SDK_INT >= 30) {
            ContentResolver resolver = context.getContentResolver();
            Uri uri = getExistingFileUri(context, SAVEFILE, "Documents/SonicJump/");
if (uri == null) {
Toast.makeText(activity, "Failed to load: " + SAVEFILE, Toast.LENGTH_SHORT).show();
    return;
}
            in = resolver.openInputStream(uri);
        } else {
            File publicFile = new File(Environment.getExternalStoragePublicDirectory("Documents/SonicJump"), SAVEFILE);
            if (publicFile.exists()) {
                in = new FileInputStream(publicFile);
            }
        }

        if (in == null) {
        Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
        public void run() {
    Toast.makeText(activity, "File not found: " + SAVEFILE, Toast.LENGTH_SHORT).show();
    }
    });
    return;
}

        Log.v("OPEN: ", SAVEFILE);

        final int bytesRead = in.read(highscores);
        if (bytesRead != highscores.length) {
            in.close();
            return;
        }

        final byte[] buffer = new byte[4];

        if (in.read(buffer) != 4) { in.close(); return; }
        speedtime = bytesToInt(buffer);

        if (in.read(buffer) != 4) { in.close(); return; }
        speedtime2 = bytesToInt(buffer);

        if (in.read(buffer) != 4) { in.close(); return; }
        eventrings2 = bytesToInt(buffer);

        if (in.read(buffer) != 4) { in.close(); return; }
        eventbluehoops2 = bytesToInt(buffer);

        in.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

    private int bytesToInt(byte[] bytes) {
    return (bytes[0] & 0xFF) << 24 | 
           (bytes[1] & 0xFF) << 16 | 
           (bytes[2] & 0xFF) << 8  | 
           (bytes[3] & 0xFF);
}

private Uri getExistingFileUri(Context context, String fileName, String relativePath) {
    String fixedRelativePath = relativePath.endsWith("/") ? relativePath : relativePath + "/";
    String[] projection = { MediaStore.MediaColumns._ID };
    String selection = MediaStore.MediaColumns.DISPLAY_NAME + "=? AND " +
                   MediaStore.MediaColumns.RELATIVE_PATH + "=? AND " +
                   MediaStore.MediaColumns.IS_PENDING + "=?";
    String[] selectionArgs = new String[] { fileName, fixedRelativePath, "0" };
    
    Cursor cursor = context.getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, selection, selectionArgs, null);
    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
                return ContentUris.withAppendedId(MediaStore.Files.getContentUri("external"), id);
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

    private void SaveGame() {
    if (!Paying.isPayed(0)) return;

    Context context = activity;
    OutputStream out = null;
    Uri targetUri = null;
    //Uri collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);

    try {
        if (Build.VERSION.SDK_INT >= 30) { //android 10+ (viu como dificultou o acesso a um único arquivo?kk)
            final String relativePath = "Documents/SonicJump/";
            targetUri = getExistingFileUri(context, SAVEFILE, relativePath);

            ContentResolver resolver = context.getContentResolver();

            if (targetUri == null) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DISPLAY_NAME, SAVEFILE);
                values.put(MediaStore.MediaColumns.MIME_TYPE, "application/octet-stream");
                values.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);
                values.put(MediaStore.MediaColumns.IS_PENDING, 1);

                targetUri = resolver.insert(MediaStore.Files.getContentUri("external"), values);
                if (targetUri == null) throw new IOException("Failed to create file in MediaStore.");
                }

            out = resolver.openOutputStream(targetUri, "rwt");
            if (out == null) throw new IOException("Failed to open OutputStream.");
        } else {
            File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "SonicJump");
            if (!directory.exists()) directory.mkdirs();

            File outFile = new File(directory, SAVEFILE);
            out = new FileOutputStream(outFile);
        }

        Log.v("SAVE", SAVEFILE);
        out.write(highscores);
        out.write(intToBytes(speedtime));
        out.write(intToBytes(speedtime2));
        out.write(intToBytes(eventrings2));
        out.write(intToBytes(eventbluehoops2));
        out.close();
        out = null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ignored) {
            }
        }
        if (Build.VERSION.SDK_INT >= 30 && targetUri != null && isPending(context, targetUri)) {
        ContentValues finish = new ContentValues();
        finish.put(MediaStore.MediaColumns.IS_PENDING, 0);
        context.getContentResolver().update(targetUri, finish, null, null);
    }
    }
}

private boolean isPending(Context context, Uri uri) {
    String[] projection = new String[] { MediaStore.MediaColumns.IS_PENDING };
    try (Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null)) {
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndex(MediaStore.MediaColumns.IS_PENDING);
            return cursor.getInt(index) == 1;
        }
    }
    return false; // Se não encontrar, considera não pendente.
}

    public byte[] intToBytes(int value) {
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (value >> 24);
    bytes[1] = (byte) (value >> 16);
    bytes[2] = (byte) (value >> 8);
    bytes[3] = (byte) value;
    return bytes;
}

public void SaveEventFile() {
    try (FileOutputStream fos = new FileOutputStream(new File(Shell.sInstance.getExternalFilesDir(null), "Event.txt"))) {
        fos.write((eventunb + eventtextures).getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static String ReadEventFile() {
    File file = new File(Shell.sInstance.getExternalFilesDir(null), "Event.txt");
    if (!file.exists()) {
        return "";
    }
    try (FileInputStream fis = new FileInputStream(file);
         ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        String content = bos.toString();
        if (content.isEmpty()) {
            return "";
        }
        return content;
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

    public void initText(String resourceName) {
        try {
            /*InputStream is = this.m_res.openRawResource(R.raw.class.getField("lang").getInt(null));
            this.ldin = new DataInputStream(is);*/
            int resourceId = this.m_res.getIdentifier(resourceName, "raw", this.activity.getPackageName());
            this.ldin = new DataInputStream(this.m_res.openRawResource(resourceId));
            localText = new String[this.ldin.readInt()];
            for (int i = 0; i < localText.length; i++) {
                String retrievedString = this.ldin.readUTF();
                if (i == GFX_ZONE_5_ENEMY_A) {
                }
                if (i == 201) {
                }
                if (i == 182) {
                }
                localText[i] = retrievedString;
            }
            this.ldin.close();
        } catch (Exception e) {
        }
    }

    public String specialCharacterReplace(char c) {
        this.ch = c;
        switch (this.ch) {
            case 161:
                this.returnString = "+";
                break;
            case 162:
            case 163:
            case 164:
            case 165:
            case 166:
            case 167:
            case 168:
            case 169:
            case 170:
            case 171:
            case 172:
            case 173:
            case 174:
            case 175:
            case 176:
            case 177:
            case 178:
            case 179:
            case 180:
            case 181:
            case 182:
            case 183:
            case 184:
            case 185:
            case 186:
            case 187:
            case 188:
            case 189:
            case 190:
            case 197:
            case 198:
            case 208:
            case 215:
            case 216:
            default:
                this.returnString = new String(new char[]{(char) this.ch});
                break;
            case 191:
                this.returnString = "-";
                break;
            case 192:
                this.returnString = "Aj";
                break;
            case 193:
                this.returnString = "Ai";
                break;
            case 194:
                this.returnString = "Ah";
                break;
            case 195:
                this.returnString = "Af";
                break;
            case 196:
                this.returnString = "Ag";
                break;
            case 199:
                this.returnString = "kC";
                break;
            case 200:
                this.returnString = "Ej";
                break;
            case 201:
                this.returnString = "Ei";
                break;
            case 202:
                this.returnString = "Eh";
                break;
            case 203:
                this.returnString = "Eg";
                break;
            case 204:
                this.returnString = "Ij";
                break;
            case 205:
                this.returnString = "Ii";
                break;
            case 206:
                this.returnString = "Ih";
                break;
            case 207:
                this.returnString = "Ig";
                break;
            case 209:
                this.returnString = "If";
                break;
            case 210:
                this.returnString = "Oj";
                break;
            case 211:
                this.returnString = "Oi";
                break;
            case 212:
                this.returnString = "Oh";
                break;
            case 213:
                this.returnString = "Of";
                break;
            case 214:
                this.returnString = "Og";
                break;
            case 217:
                this.returnString = "Uj";
                break;
            case 218:
                this.returnString = "Ui";
                break;
            case 219:
                this.returnString = "Uh";
                break;
            case 220:
                this.returnString = "Ug";
                break;
            case 221:
                this.returnString = "Yi";
                break;
        }
        return this.returnString;
    }

    public char stringReplace(char c) {
        this.ch = c;
        switch (this.ch) {
            case 33:
                this.ch2 = GFX_BOSS_NEGATIVE_4;
                break;
            case 34:
                this.ch2 = GFX_ANTENNA_BASE;
                break;
            case GFX_SPINNER_POLE:
                this.ch2 = GFX_BOSS_NEGATIVE_6;
                break;
            case GFX_ANTENNA_OFF:
                this.ch2 = GFX_SWISH_A_FLIP;
                break;
            case GFX_ANTENNA_ON:
                this.ch2 = GFX_SWISH_B_FLIP;
                break;
            case GFX_EXPLOSION_A:
                this.ch2 = GFX_BOSS_NEGATIVE_3;
                break;
            case GFX_ZONE_1_ENEMY_A:
                this.ch2 = 101;
                break;
            case GFX_ZONE_1_ENEMY_B:
                this.ch2 = GFX_GREEN_BONUS_FRONT;
                break;
            case GFX_ZONE_2_ENEMY_A:
                this.ch2 = GFX_GREEN_BONUS_GO_B;
                break;
            case GFX_ZONE_2_ENEMY_A_FLIP:
                this.ch2 = GFX_ZONE_1_ENEMY_A;
                break;
            case GFX_BOSS_NEGATIVE_3:
                this.ch2 = GFX_SONIC_DEAD_FLIP;
                break;
            case 118:
                this.ch2 = GFX_BLUE_BONUS_GO_A;
                break;
            case GFX_BALL2:
                this.ch2 = GFX_GREEN_BONUS_GO_A;
                break;
            case GFX_CRUSHER2:
                this.ch2 = GFX_EXPLOSION_A;
                break;
            case GFX_BALL3:
                this.ch2 = GFX_SONIC_DEAD;
                break;
            case GFX_BALL4:
                this.ch2 = GFX_SPRING_SQUASHED;
                break;
            default:
                this.ch2 = this.ch;
                break;
        }
        char c2 = (char) this.ch2;
        return c2;
    }

    public static String replaceText(String source, String replace, String by) {
        int place = source.indexOf(replace);
        if (place != -1) {
            String returnString = String.valueOf(source.substring(0, place)) + by + source.substring(replace.length() + place);
            return returnString;
        }
        return source;
    }

    public void initLanguage(int lang) {
        String languageChosen = "";
        switch (lang) {
            case 1:
                languageChosen = "lang";
                break;
            case 2:
                languageChosen = "fr";
                break;
            case 3:
                languageChosen = "it";
                break;
            case 4:
                languageChosen = "de";
                break;
            case 5:
                languageChosen = "es";
                break;
            case 6:
                languageChosen = "pt";
                break;
            case 7:
                languageChosen = "br";
                break;
            case 8:
                languageChosen = "jp";
                break;
            case 9:
                languageChosen = "ch";
                break;
        }
        CurrentL = languageChosen;
        loadCutscenesIni(Shell.sInstance, "shadow_campaign/cutscenes_" + CurrentL + ".ini");
        initText(languageChosen);
    }

    public void setupCS() {
        this.cslist = new int[]{1, 0, 0, GFX_ZONE_5_ENEMY_B, -1, 2, 0, 0, GFX_ZONE_5_ENEMY_A_FLIP, 0, 2, 0, 0, GFX_ZONE_5_ENEMY_A_FLIP, 0, 2, 12, 0, GFX_ZONE_5_ENEMY_B_FLIP, 0, 2, 11, 0, GFX_BOSS_1, 0, 3, 15, 0, GFX_BOSS_2, 0, 2, 9, 0, GFX_BOSS_3, 0, 3, 15, 0, GFX_BOSS_4, 0, 2, 11, 0, GFX_BOSS_5, 0, 3, 15, 0, GFX_BOSS_6, 0, 2, 11, 0, GFX_SMOKE_1, 0, 3, 14, 0, GFX_SMOKE_2, 0, 2, 11, 0, GFX_SMOKE_3, 0, 2, 9, 0, GFX_SMOKE_4, -2, 3, 11, 0, GFX_SMOKE_5, 0, 3, 12, 0, GFX_SMOKE_6, 0, 3, 12, 21, GFX_SHARD_1, 0, 3, 12, 21, GFX_SHARD_2, 0, 3, 12, 20, GFX_SHARD_3, 0, 3, 23, 0, GFX_SHARD_4, -3, 4, 12, 0, GFX_BOSS_NEGATIVE_1, 0, 4, 12, 0, GFX_BOSS_NEGATIVE_2, -3, 5, 12, 13, GFX_BOSS_NEGATIVE_3, 0, 5, 12, 22, GFX_BOSS_NEGATIVE_4, 0, 5, 12, 22, GFX_BOSS_NEGATIVE_5, 0, 5, 12, 21, GFX_BOSS_NEGATIVE_6, 0, 5, 12, 0, GFX_SWISH_A_FLIP, 0, 5, 12, 15, GFX_SWISH_B_FLIP, 0, 5, 12, 15, 100, 0, 5, 12, 15, 101, 0, 5, 12, 15, 102, 0, 5, 11, 15, 103, -3, 6, 26, 20, 104, 0, 6, 12, 0, 105, 0, 6, 18, 20, 106, 0, 6, 18, 21, 107, 0, 6, 26, 21, 108, 0, 6, 26, 21, 109, 0, 6, 12, 21, 110, 0, 6, 12, 20, 111, 0, 6, 12, 20, 112, 0, 6, 12, 20, 113, 0, 6, 12, 21, 114, 0, 6, 14, 0, 115, 0, 6, 19, 0, 116, 0, 6, 12, 19, 117, 0, 6, 15, 0, 118, 0, 6, 14, 0, GFX_BALL, 0, 6, 14, 0, GFX_CRUSHER, 0, 6, 11, 14, GFX_BALL2, 0, 6, 12, 15, GFX_CRUSHER2, 0, 6, 12, 14, GFX_BALL3, 0, 6, 10, 14, GFX_RESULTS_SHARD_BLACK, -3, 7, 20, 0, GFX_BALL4, 0, 7, 12, 20, GFX_BALL5, 0, 7, 12, 21, GFX_BALL6, 0, 7, 12, 20, 128, 0, 7, 24, 0, 169, 0, 7, 11, 0, 129, 0, 7, 11, 15, 130, 0, 7, 9, 15, 131, 0, 7, 9, 14, 132, 0, 7, 10, 14, 133, 0, 7, 10, 17, 134, 0, 7, 12, 0, 135, -3, 8, 22, 0, 136, 0, 8, 10, 22, 137, 0, 8, 10, 22, 138, 0, 8, GFX_SONIC_SPINNING_C_FLIP, 0, 139, 0, 8, GFX_SONIC_SPINNING_C_FLIP, 0, 140, 0, 3, 15, 0, 141, 0, 3, 11, 15, 142, 0, 3, 11, 13, 143, 0, 3, 9, 17, 144, 0, 3, 10, 17, 145, 0, 3, 10, 16, 146, 0, 3, 9, 16, 147, 0, 3, 9, 19, 148, 0, 3, 9, 19, 149, 0, 3, 9, 19, 150, 0, 3, 10, 19, 151, 0, 3, 10, 19, 152, -1, 3, 15, 0, 153, 0, 3, 12, 15, 154, 0, 3, 12, 19, 155, 0, 3, 11, 19, 156, -2, 8, 12, 0, 157, 0, 8, 12, 20, 158, 0, 8, 11, 20, 159, -1, 8, 9, 0, 160, 0, 8, 9, 20, 161, 0, 8, 10, 20, 162, 0, 8, 9, 22, 163, 0, 8, 12, 0, 164, 0, 3, 15, 0, 165, 0, 3, 9, 13, 166, 0, 3, 9, 16, 167, 0, 3, 9, 16, 168, -6};
    }

    public void processEndDemo() {
        setlock(1);
        this.ssely = 0;
        if (this.res != -1) {
            this.musicwanted = -1;
        }
        if (vodaDemo.equals("0") || vodaDemo == null || vodaDemo == "") {
            this.textheading = 190;
            this.stringmax = this.GameWidth - GFX_BLUE_BONUS_GO_A;
            if (demoURL == null || demoURL == "") {
                inittext(localText[185]);
            } else if (this.demoMode.equals("2")) {
                inittext(localText[183]);
            } else {
                inittext(localText[182]);
            }
        }
    }

    public void processSound(int task, int param1, int param2) {
        switch (task) {
            case 0:
                this.DeviceSound = new DeviceSound();
                return;
            case 1:
                if (this.SoundAvailable != 0 && this.SoundOnOff != 0 && this.musicwanted > 0) {
                    this.mWanted = this.musicwanted;
                    this.DeviceSound.PlaySound(param1, param2);
                    if (bluehoops == eventbluehoops) {
                            eventbluehoops2 = 1;
                            SaveGame();
                            }
                    return;
                }
                return;
            case 2:
                if (this.SoundAvailable != 0) {
                    this.DeviceSound.PlaySound(0, 1);
                    if (bluehoops == eventbluehoops) {
                            eventbluehoops2 = 1;
                            SaveGame();
                            }
                    return;
                }
                return;
            case 3:
            default:
                return;
            case SOUND_PLAY_STORED:
                if (this.SoundAvailable != 0 && this.SoundOnOff != 0) {
                    DeviceSound.PlayStoredSound();
                    return;
                }
                return;
        }
    }

    public int getLastSound() {
        return DeviceSound.getLastSound();
    }

    public int getMusicWanted() {
        return this.musicwanted;
    }
    
    public Graphics getG() {
        return this.G;
    }

    public int getQed() {
        return DeviceSound.getQed();
    }

    public void zoneSelectTouch(int x, int y) {
        System.out.println("zone: " + x + "," + y + " " + (this.highscores[((y * 3) + 10) + x] != 0));
        if (this.highscores[(y * 3) + 10 + x] != 0) {
            this.lselx = x;
            this.lsely = y;
            this.key_fire = 1;
        }
    }

    public void pointMenu(int _menu, int _id, int _key) {
        flashTimer = 9;
        this.menu[_menu] = (byte) _id;
    }

    public void flushTouch() {
        if (flashTimer <= 0) {
            this.key_fire = 1;
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
    if (PauseGameTouch) {
    return true;
    }
        /*float x;
        if (!multip)
        {
        x = event.getX();
        } else {
        x = event.getX() / 2;
        }*/
        float x = 0;
        float y = 0;
        if (!rotationland) {
        x = event.getX();
        y = event.getY();
        } else {
        y = event.getY();
        x = event.getX();
        }
        event.getXPrecision();
        event.getYPrecision();
        float x2 = x * this.scale_x;
        float y2 = y * this.scale_y;
        _T(String.valueOf(x2) + "  " + y2);
        _T(String.valueOf(event.getXPrecision()) + "  " + event.getYPrecision());
        switch (event.getAction()) {
            case 0:
                pointerPressed((int) x2, (int) y2, event);
                return true;
            case 1:
                pointerReleased((int) x2, (int) y2);
                return true;
            default:
                return true;
        }
    }

    public void pointerPressed(int x, int y, MotionEvent event) {
    int numOptions;
    int sectionHeight;
    int startY;
        if (LoopMode == -1 || LoopMode == -2 || this.m_bPaying) {
            if (y > 600 && y < 640 && x > 280 && x < 360) {
                this.choice = 2;
                return;
            } else {
                if (y > 600 && y < 640 && x > 0 && x < GFX_BOSS_5) {
                    this.choice = 1;
                    return;
                }
                return;
            }
        }
        if (flashTimer >= 10 || flashTimer < 0) {
            if (LoopMode == 1) {
                if (y > 0 && y < GFX_ZONE_5_ENEMY_A && x > GFX_CRUSHER && x < 240 && this.tut == 0) {
                    keyPressed(2);
                    touchState |= 8;
                }
                if (this.TouchpadVisibleOnOff == 0) {
                    if (y > this.b_mid2.top && y < this.b_mid2.bottom && x > this.b_mid2.left && x < this.b_mid2.right) {
                        keyPressed(GFX_ZONE_3_ENEMY_A);
                        touchState |= 4;
                        return;
                    }
                    return;
                }
                if (y > this.b_right.top && y < this.b_right.bottom && x > this.b_right.left && x < this.b_right.right) {
                    keyPressed(15);
                    touchState |= 1;
                    return;
                }
                if (y > this.b_left.top && y < this.b_left.bottom && x > this.b_left.left && x < this.b_left.right) {
                    keyPressed(9);
                    touchState |= 2;
                    return;
                }
                if (y > this.b_right.top -50 && y < this.b_right.bottom -130 && x > this.b_right.left && x < this.b_right.right) {
                    keyPressed(15);
                    keyPressed(GFX_ZONE_3_ENEMY_A);
                    newTouch = true;
                    touchState |= 1;
                    return;
                } else {
                if (event.getAction() != MotionEvent.ACTION_MOVE && newTouch) {
        newTouch = false;
        }
                /*if (newTouch) {
            newTouch = false;
            }*/
            }
                if (y > this.b_left.top -50 && y < this.b_left.bottom -130 && x > this.b_left.left && x < this.b_left.right) {
                    keyPressed(9);
                    keyPressed(GFX_ZONE_3_ENEMY_A);
                    newTouch = true;
                    touchState |= 2;
                    return;
                } else {
                if (event.getAction() != MotionEvent.ACTION_MOVE && newTouch) {
        newTouch = false;
        }
                /*if (newTouch) {
            newTouch = false;
            }*/
                    if (y > this.b_mid1.top && y < this.b_mid1.bottom && x > this.b_mid1.left && x < this.b_mid1.right) {
                        keyPressed(GFX_ZONE_3_ENEMY_A);
                        touchState |= 4;
                        return;
                    }
                    return;
                }
            }
            switch (LoopMode) {
                case 42:
                numOptions = 4;
this.initPosY = 262;
for (int i = 0; i < numOptions; i++) {
    sectionHeight = 35;
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(42, i, this.key_fire);
        return;
    }
    }
if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        } else {
                        return;
                        }
                case 41:
                    this.initPosY = 291/*339*/;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(41, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(41, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 39:
                    this.initPosY = 284;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(39, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(39, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 38:
                    this.initPosY = 296;
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                        }
                        return;
                case 37:
                    this.initPosY = 284/*339*/;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(37, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(37, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 36:
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        } else {
                        pointMenu(36, 0, this.key_fire);
                        }
                        return;
                case 35:
                this.initPosY = 279/*339*/;
                numOptions = 3;
                    for (int i = 0; i < numOptions; i++) {
    sectionHeight = 40;
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(35, i, this.key_fire);
        return;
    }
    }
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                case 2:
                    //this.initPosY = (this.GameHeight - 175) / 2;
if (multip) {
numOptions = 2;
this.initPosY = 280;
} else {
if (this.bonusunlocked == 1 || backPressCount >= 7) {
if (!DemoFurikan) {
numOptions = 11 + 2;
this.initPosY = 140 -34;
} else {
numOptions = 5;
this.initPosY = 243;
}
} else {
if (!DemoFurikan) {
numOptions = 10 + 1;
this.initPosY = 157 -17;
} else {
numOptions = 5;
this.initPosY = 243;
}
}
}
/*for (int i = 0; i < numOptions; i++) {
    if (i >= 0 && i <= 2) {
    if (numOptions != 9) {
    sectionHeight = 17; if (i == 1) { sectionHeight = 12; } if (i == 0) { sectionHeight = 10; }
    } else { sectionHeight = 12; if (i == 1) { sectionHeight = 10; } if (i == 0) { sectionHeight = 8; }
    }
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(2, i, this.key_fire);
        return;
    }
    } else {
    if (numOptions != 9) {
    sectionHeight = 30;
    } else { sectionHeight = 25; }
    startY = this.initPosY + (i * sectionHeight);
    int endY2 = startY + sectionHeight;
    if (y > startY && y < endY2) {
        pointMenu(2, i, this.key_fire);
        return;
    }
    }
}*/
for (int i = 0; i < numOptions; i++) {
    sectionHeight = 35;
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(2, i, this.key_fire);
        return;
    }
    }
return;
                    // y > this.initPosY + button * 35 ; y < this.initPosY + button * 35 + 35
                    /*if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(2, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(2, 1, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_ZONE_4_ENEMY_A && y < this.initPosY + 105) {
                        pointMenu(2, 2, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 105 && y < this.initPosY + 140) {
                        pointMenu(2, 3, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 140 && y < this.initPosY + 175) {
                        pointMenu(2, 4, this.key_fire);
                        return;
                    } //else {
                        if (y > this.initPosY + 175 && y < this.initPosY + 210) {
                            pointMenu(2, 5, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + 210 && y < this.initPosY + 245) {
                            pointMenu(2, 6, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + 245 && y < this.initPosY + 280) {
                            pointMenu(2, 7, this.key_fire);
                            return;
                        }
                        //return;
                    //}*/
                    case 3:
                    //this.initPosY = (this.GameHeight - 175) / 2;
                    this.initPosY = 192 -17;
numOptions = 9;
/*for (int i = 0; i < numOptions; i++) {
    if (i >= 0 && i <= 1) {
    sectionHeight = 2;
    if (i == 0) { sectionHeight = 5; }
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(3, i, this.key_fire);
        return;
    }
    } else {
    sectionHeight = 25;
    startY = this.initPosY + (i * sectionHeight);
    int endY2 = startY + sectionHeight;
    if (y > startY && y < endY2) {
        pointMenu(3, i, this.key_fire);
        return;
    }
    }
}*/
for (int i = 0; i < numOptions; i++) {
    sectionHeight = 35;
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(3, i, this.key_fire);
        return;
    }
    }
if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        } else {
                        return;
                        }
                    /*if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(3, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(3, 1, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_ZONE_4_ENEMY_A && y < this.initPosY + 105) {
                        pointMenu(3, 2, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 105 && y < this.initPosY + 140) {
                        pointMenu(3, 3, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 140 && y < this.initPosY + 175) {
                        pointMenu(3, 4, this.key_fire);
                        return;
                    } //else {
                        if (y > this.initPosY + 175 && y < this.initPosY + 210) {
                            pointMenu(3, 5, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + 210 && y < this.initPosY + 245) {
                            pointMenu(3, 6, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + 245 && y < this.initPosY + 280) {
                            pointMenu(3, 7, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + 280 && y < this.initPosY + 315) {
                            pointMenu(3, 8, this.key_fire);
                            return;
                        }
                        //return;
                    //}*/
                case SOUND_PLAY_STORED:
                    if (x > 0 && x < 110 && y > 280 && y < 395) {
                        this.key_left = 1;
                        return;
                    }
                    if (x > 250 && x < 360 && y > 280 && y < 395) {
                        this.key_right = 1;
                        return;
                    }          
                    if (x > 110 && x < 250 && y > 200 && y < 280) {
                        this.key_up = 1;
                        return;
                    }
                    if (x > 110 && x < 250 && y > 410 && y < 490) {
                        this.key_down = 1;
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 5:
                    this.initPosY = 160;
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY && y < this.initPosY + GFX_SPRING_SQUASHED) {
                        zoneSelectTouch(0, 0);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY && y < this.initPosY + GFX_SPRING_SQUASHED) {
                        zoneSelectTouch(1, 0);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY && y < this.initPosY + GFX_SPRING_SQUASHED) {
                        zoneSelectTouch(2, 0);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + GFX_SPRING_SQUASHED && y < this.initPosY + 102) {
                        zoneSelectTouch(0, 1);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + GFX_SPRING_SQUASHED && y < this.initPosY + 102) {
                        zoneSelectTouch(1, 1);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + GFX_SPRING_SQUASHED && y < this.initPosY + 102) {
                        zoneSelectTouch(2, 1);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + 102 && y < this.initPosY + 153) {
                        zoneSelectTouch(0, 2);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + 102 && y < this.initPosY + 153) {
                        zoneSelectTouch(1, 2);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + 102 && y < this.initPosY + 153) {
                        zoneSelectTouch(2, 2);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + 153 && y < this.initPosY + 204) {
                        zoneSelectTouch(0, 3);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + 153 && y < this.initPosY + 204) {
                        zoneSelectTouch(1, 3);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + 153 && y < this.initPosY + 204) {
                        zoneSelectTouch(2, 3);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + 204 && y < this.initPosY + 255) {
                        zoneSelectTouch(0, 4);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + 204 && y < this.initPosY + 255) {
                        zoneSelectTouch(1, 4);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + 204 && y < this.initPosY + 255) {
                        zoneSelectTouch(2, 4);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + 255 && y < this.initPosY + 306) {
                        zoneSelectTouch(0, 5);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + 255 && y < this.initPosY + 306) {
                        zoneSelectTouch(1, 5);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + 255 && y < this.initPosY + 306) {
                        zoneSelectTouch(2, 5);
                        return;
                    }
                    if (x > GFX_EXPLOSION_D && x < 123 && y > this.initPosY + 306 && y < this.initPosY + 357) {
                        zoneSelectTouch(0, 6);
                        return;
                    }
                    if (x >= 123 && x < 203 && y > this.initPosY + 306 && y < this.initPosY + 357) {
                        zoneSelectTouch(1, 6);
                        return;
                    }
                    if (x >= 203 && x < 278 && y > this.initPosY + 306 && y < this.initPosY + 357) {
                        zoneSelectTouch(2, 6);
                        return;
                    }
                    if ((this.sk1 == 0 || x <= 0 || x >= GFX_SONIC_DEAD_FLIP || y <= 600 || y >= 640) && this.sk1 != 0 && x > 280 && x < 360 && y > 600 && y < 640) {
                        keyPressed(2);
                        return;
                    }
                    return;
                case 6:
                    this.initPosY = (this.GameHeight - GFX_HUD_LIVES) / 2;
                    if (this.livesshown == 0) {
                        if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                            pointMenu(6, 0, this.key_fire);
                            return;
                        }
                        if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                            pointMenu(6, 1, this.key_fire);
                            return;
                        }
                        if (x <= 0 || x >= GFX_SONIC_DEAD_FLIP || y > 600) {
                        }
                        return;
                    }
                    this.key_fire = 1;
                    return;
                case 8:
                    this.initPosY = ((this.GameHeight - GFX_ZONE_4_ENEMY_A) / 2) + GFX_HUD_LIVES;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(8, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(8, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 13:
                    this.initPosY = (this.GameHeight - 210) / 2;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(13, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(13, 1, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_ZONE_4_ENEMY_A && y < this.initPosY + 105) {
                        pointMenu(13, 2, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 105 && y < this.initPosY + 140) {
                        pointMenu(13, 3, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 140 && y < this.initPosY + 175) {
                        pointMenu(13, 4, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 175 && y < this.initPosY + 210) {
                        pointMenu(13, 5, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 210 && y < this.initPosY + 245) {
                        pointMenu(13, 6, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 15:
                    //this.initPosY = (this.GameHeight - 105) / 2;
//int altt = this.bonusunlocked == 1 || backPressCount >= 7 ? 1 : 0;
numOptions = 9 /*9 + altt*/;
this.initPosY = 192/*altt == 1 ? 157 : 175*/;
for (int i = 0; i < numOptions; i++) {
    sectionHeight = 35;
    startY = this.initPosY + (i * sectionHeight);
    int endY = startY + sectionHeight;
    if (y > startY && y < endY) {
        pointMenu(15, i, this.key_fire);
        return;
    }
    }
if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        } else {
                        return;
                        }
                    /*if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(15, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(15, 1, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_ZONE_4_ENEMY_A && y < this.initPosY + 105) {
                        pointMenu(15, 2, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 105 && y < this.initPosY + 140) {
                        pointMenu(15, 3, this.key_fire);
                        return;
                    } //else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        if (y > this.initPosY + 140 && y < this.initPosY + 175) {
                        pointMenu(15, 4, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 175 && y < this.initPosY + 210) {
                        pointMenu(15, 5, this.key_fire);
                        return;
                    }
                        //return;
                    //}*/
                case 16:
                    if (y > 200 && y < 250) {
                        this.key_up = 1;
                        return;
                    }
                    if (y > 407 && y < 457) {
                        this.key_down = 1;
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 17:
                    this.initPosY = 279;//((this.GameHeight - GFX_HUD_LIVES) / 2);
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(17, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(17, 1, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + 70 && y < this.initPosY + 105) {
                        pointMenu(17, 2, this.key_fire);
                        return;
                    }
                    if (x <= 0 || x >= GFX_SONIC_DEAD_FLIP || y > 600) {
                    }
                    return;
                case 21:
                    this.initPosY = 339;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(21, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(21, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 26:
                    this.initPosY = ((this.GameHeight - GFX_ZONE_4_ENEMY_A) / 2) + GFX_HUD_LIVES;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(26, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(26, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 28:
                    this.initPosY = 291/*339*/;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(28, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(28, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                case 34:
                    this.initPosY = (this.GameHeight - GFX_HUD_LIVES) / 2;
                    if (y > this.initPosY && y < this.initPosY + GFX_HUD_LIVES) {
                        pointMenu(34, 0, this.key_fire);
                        return;
                    }
                    if (y > this.initPosY + GFX_HUD_LIVES && y < this.initPosY + GFX_ZONE_4_ENEMY_A) {
                        pointMenu(34, 1, this.key_fire);
                        return;
                    } else {
                        if (x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        }
                        return;
                    }
                default:
                    if (this.sk1 == 0 || x <= 0 || x >= GFX_BOSS_5 || y <= 600 || y >= 640) {
                        if (this.sk2 != 0 && x > 280 && x < 360 && y > 600 && y < 640) {
                            keyPressed(2);
                            return;
                        } else {
                            this.key_fire = 1;
                            return;
                        }
                    }
                    return;
            }
        }
    }

    private void keyPressed(int key) {
        onKeyDown(key, null);
    }

    public void pointerReleased(int x, int y) {
        this.MenuTimer = 0;
        touchState = 0;
        clearkeys();
    }

    public void drawSonic() {
    }

    public static final void _T(String p_data) {
        Log.v("Game", p_data);
    }

    public void setPaying(int type) {
        this.m_bPaying = true;
        this.m_PayingType = type;
        clearkeys();
        ResumeSound = DeviceSound.getLastSound();
        processSound(2, 0, 0);
        this.musicwanted = -1;
        processSound(1, -1, 1);
    }
    
    public void CRTEnabled() {
    Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
        public void run() {
    if (crt || crt2 != 0) {
    DisplayMetrics metrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= 17) {
    Shell.sInstance.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
} else {
    Shell.sInstance.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
int height = metrics.heightPixels;
crtView = new CRTView(Shell.sInstance);
crtView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
frameLayout = new FrameLayout(Shell.sInstance);
        ViewGroup parent = (ViewGroup) Shell.sInstance.getCanvas().getParent();
        if (parent != null) {
    parent.removeView(Shell.sInstance.getCanvas());
    } else { 
return;
}
        frameLayout.addView(Shell.sInstance.getCanvas());
        frameLayout.addView(crtView);
        Shell.sInstance.setContentView(frameLayout);
        try {
        if (crt2 == 1) {
        new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    while (crt2 != 0) {
    for (int i = 1; i <= 3; i++) {
    final String str = i == 1 ? "crt2.png" : i == 2 ? "crt2-1.png" : "crt2-2.png";
    Shell.sInstance.runOnUiThread(new Runnable() {
        @Override
        public void run() {
        try {
        crtView.setReceivedBitmap(BitmapFactory.decodeStream(Shell.sInstance.getAssets().open(str)));
        } catch (IOException e) {
        }
        }
        });
        Thread.sleep(166);
    }
    }
    if (crtView != null && frameLayout != null) crtView.disconnect();
    } catch (Exception e) {
    }
    }
    }).start();
        } else {
        crtView.setReceivedBitmap(BitmapFactory.decodeStream(Shell.sInstance.getAssets().open("crt.png")));
        }
        } catch (IOException e) {
        }
        } else {
        if (crtView != null && frameLayout != null) crtView.disconnect();
        }
        }
    });
    }
    
   public class CRTView extends View {
    private Bitmap receivedBitmap;
    public boolean disconnect = false;

    public CRTView(Context context) {
        super(context);
    }
    
    public void disconnect() {
    this.receivedBitmap = null;
    disconnect = true;
    invalidate();
    }

    public void setReceivedBitmap(Bitmap bitmap) {
        this.receivedBitmap = bitmap;
        invalidate();  // Redesenha a View
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (receivedBitmap != null) {
            //if (receivedBitmap.getWidth() > getWidth() && receivedBitmap.getHeight() > getHeight()) {
            Rect destRect = new Rect(0, 0, Game_Draw.WIDTH, Game_Draw.HEIGHT);
            canvas.drawBitmap(receivedBitmap, null, destRect, null);
            /*} else {
            Rect destRect = new Rect(0, 0, receivedBitmap.getWidth(), receivedBitmap.getHeight());
            canvas.drawBitmap(receivedBitmap, null, destRect, null);
            }*/
        } 
        if (disconnect) {
        canvas.drawColor(Color.TRANSPARENT); 
        }
    }
}

public class GameTask {

    private Socket socket;
    private InputStream inputStream;
    private OutputStream outputStream;
    private boolean isReceptor;
    private Bitmap receivedBitmap;
    private Graphics G;
    private boolean running = true;
    private Handler mainHandler;
    public Bitmap receive;
    public Bitmap bm;
    public boolean check = true;
    public CustomGameView customGameView;
    public FrameLayout rootLayout;
    
    public GameTask(Context context, Socket socket, boolean isReceptor, Graphics G) {
        this.G = G;
        this.socket = socket;
        this.isReceptor = isReceptor;
        this.mainHandler = new Handler(Looper.getMainLooper()); // Handler para a thread principal
        final Graphics G2 = G;

        try {
            this.inputStream = socket.getInputStream();
            this.outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        receive = G.getBitmap();
        /*int width = G.getCanvas().getWidth();
        int height = G.getCanvas().getHeight();
        G.getCanvas().scale(1f, 0.5f);
        G.getCanvas().translate(0, height);*/
        shadow = false;
        tails = false;
        sonic = true;
        if (!isReceptor) {
        SpritePic = new DeviceImage("shadow");
        } else {
                SpritePic = new DeviceImage(196619);
                }
                //ProcessDemoStart();
                Game_Draw.this.LoopModeWanted = 2;
                
                customGameView = new CustomGameView(context);
                /*customGameView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));*/
                DisplayMetrics metrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= 17) {
    Shell.sInstance.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
} else {
    Shell.sInstance.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels / 5;
int height = metrics.heightPixels / 5;
customGameView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        rootLayout = new FrameLayout(Shell.sInstance);
        ViewGroup parent = (ViewGroup) Shell.sInstance.getCanvas().getParent();
if (parent != null) {
    parent.removeView(Shell.sInstance.getCanvas()); // Remove from old parent
}
        rootLayout.addView(Shell.sInstance.getCanvas());
        rootLayout.addView(customGameView);
        Shell.sInstance.setContentView(rootLayout);

        // Thread para comunicação com o outro jogador
        new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while (running) {
            if (check) {
                enviarTela();
                }
                //receive = Game_Draw.this.G.getBitmap();
                //customGameView.setReceivedBitmap(receive);
                /*Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
            if (bm != null) {
            Game_Draw.this.G.getCanvas().drawBitmap(bm, 0, 0, null);
            }
            }
            });*/
                //Thread.sleep(160L);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            G2.getCanvas().scale(1f, 1f);
            Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
            stop();
        }
    }
}).start();
        if (!running) {
                G.getCanvas().scale(1f, 1f);
                Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
                stop();
                }
    }

    private void enviarTela() {
    receive = G.getBitmap();
    check = false;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int width = receive.getWidth();
int height = receive.getHeight();
//Bitmap rightHalf = Bitmap.createBitmap(receive, 0, height, width, height);
Bitmap rightHalf = Bitmap.createScaledBitmap(receive, width, height, false);
rightHalf.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
//receive.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);

            byte[] data = byteArrayOutputStream.toByteArray();

            // Envia o tamanho da imagem (4 bytes)
            outputStream.write(ByteBuffer.allocate(4).putInt(data.length).array());

            // Envia os bytes da imagem
            outputStream.write(data);
            outputStream.flush();
            receberTela();
        } catch (IOException e) {
        Game_Draw.this.GameHeight = Game_Draw.this.GameHeight;
        Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
        G.getCanvas().scale(1f, 1f);
        stop();
            e.printStackTrace();
        }
    }

    private void receberTela() {
        try {
            // Lê os 4 bytes do tamanho da imagem
            byte[] sizeBuffer = new byte[4];
            int bytesRead = 0;
            while (bytesRead < 4) {
                int result = inputStream.read(sizeBuffer, bytesRead, 4 - bytesRead);
                if (result <= 0) {
                Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
                G.getCanvas().scale(1f, 1f);
                    running = false;
                    stop();
                    return;
                }
                bytesRead += result;
            }

            // Converte os 4 bytes em um inteiro
            int size = ByteBuffer.wrap(sizeBuffer).getInt();

            // Lê os dados da imagem completamente
            byte[] buffer = new byte[size];
            bytesRead = 0;
            while (bytesRead < size) {
                int result = inputStream.read(buffer, bytesRead, size - bytesRead);
                if (result <= 0) {
                Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
                G.getCanvas().scale(1f, 1f);
                    running = false;
                    stop();
                    return;
                }
                bytesRead += result;
            }

            // Converte os bytes em um Bitmap
            bm = BitmapFactory.decodeByteArray(buffer, 0, size);

            Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //att();
                if (bm != null) {
                //G = new Graphics(GameWidth, GameHeight/2, 0, 0);
                //G.getCanvas().translate(0, 0);
                //G.getCanvas().drawBitmap(bm, 0, 0, null);
                customGameView.setReceivedBitmap(bm);
                //G.getCanvas().translate(0, height);
                }
                check = true;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
            stop();
            Game_Draw.this.G = new Graphics(Game_Draw.this.GameWidth, Game_Draw.this.GameHeight, 0, 0);
            G.getCanvas().scale(1f, 1f);
        }
    }

    public void stop() {
        running = false;
        multip = false;
        customGameView.disconnect();
        /*Shell.sInstance.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                rootLayout.removeView(customGameView);
        ViewGroup parent = (ViewGroup) rootLayout.getParent();
if (parent != null) {
    parent.removeView(rootLayout); // Remove from old parent
}
        Shell.sInstance.setContentView(Shell.sInstance.getCanvas());
        }
        });*/
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public class CustomGameView extends View {
    private Bitmap receivedBitmap;
    public boolean disconnect = false;

    public CustomGameView(Context context) {
        super(context);
    }
    
    public void disconnect() {
    this.receivedBitmap = null;
    disconnect = true;
    invalidate();
    }

    public void setReceivedBitmap(Bitmap bitmap) {
        this.receivedBitmap = bitmap;
        invalidate();  // Redesenha a View
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (receivedBitmap != null) {
            if (receivedBitmap.getWidth() > getWidth() && receivedBitmap.getHeight() > getHeight()) {
            Rect destRect = new Rect(0, 0, getWidth(), getHeight());
            canvas.drawBitmap(receivedBitmap, null, destRect, null);
            } else {
            Rect destRect = new Rect(0, 0, receivedBitmap.getWidth(), receivedBitmap.getHeight());
            canvas.drawBitmap(receivedBitmap, null, destRect, null);
            }
        } 
        if (disconnect) {
        canvas.drawColor(Color.TRANSPARENT); 
        }
    }
}
}
class IniCutscene {
    public List<IniImage> image = new ArrayList<>();
    public List<String> text = new ArrayList<>();
    public int action = 0;
}
class IniImage {
    public String path = "";
    public String pos = "";
    public boolean flip;
    public List<Integer> showOn = new ArrayList<>(); // <- textos onde aparece
}
}